File tree 1 file changed +8
-9
lines changed 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change @@ -1443,19 +1443,18 @@ BFS,使用队列,用一个标志位来决定是否需要翻转。
1443
1443
高度平衡意味着每次必须选择中间数字作为根节点。
1444
1444
1445
1445
class Solution {
1446
+ public TreeNode sortedArrayToBST(int[] nums) {
1447
+ return buildBST(nums, 0, nums.length-1);
1448
+ }
1446
1449
1447
- TreeNode helper (int left , int right , int[] nums ){
1448
- if(left > right ) return null;
1449
- int mid = (left + right) / 2;
1450
+ private TreeNode buildBST (int[] nums , int start , int end ){
1451
+ if(start > end ) return null;
1452
+ int mid = (start + end)/ 2;
1450
1453
TreeNode root = new TreeNode(nums[mid]);
1451
- root.left = helper(left, mid - 1, nums );
1452
- root.right = helper( mid+1, right, nums );
1454
+ root.left = buildBST(nums,start, mid-1 );
1455
+ root.right = buildBST(nums, mid+1,end );
1453
1456
return root;
1454
1457
}
1455
-
1456
- public TreeNode sortedArrayToBST(int[] nums) {
1457
- return helper(0,nums.length-1,nums);
1458
- }
1459
1458
}
1460
1459
1461
1460
## 7. 根据有序链表构造平衡的二叉查找树
You can’t perform that action at this time.
0 commit comments