1. 程式人生 > >108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

order binary node arr where balance logs roo search

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

 1 class Solution {
 2     public TreeNode sortedArrayToBST(int[] nums) {
 3          return nums.length==0? null : ToBST(nums,0,nums.length-1);
 4         
 5         
 6     }
 7     public TreeNode ToBST(int
[] nums,int lo,int hi) { 8 if(lo>hi) return null; 9 int mid = (lo+hi)/2; 10 TreeNode root = new TreeNode(nums[mid]); 11 root.left = ToBST(nums,lo,mid-1); 12 root.right = ToBST(nums,mid+1,hi); 13 14 return root; 15 16 } 17 }

108. Convert Sorted Array to Binary Search Tree