react 前端框架如何驱动企业数字化转型与创新发展
583
2022-10-09
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}public class Solution { public TreeNode sortedArrayToBST(int[] num) { int len = num.length; if(len<=0) return null; return sorted(num, 0, len-1); } public TreeNode sorted(int[] num, int start, int end){ if(start>end) return null; int mid = (start + end) / 2 + (start + end) % 2; TreeNode root = new TreeNode(num[mid]); root.left = sorted(num, start, mid-1); root.right = sorted(num, mid+1, end); return root; } }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~