对给定的升序数组,将其转化成深度最低的二叉搜索树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0) return null;
return helper(nums,0,nums.length - 1);
}
public TreeNode helper(int[] nums,int low,int hight){
int mid = (low + hight)/2;
if(low > hight) return null;
TreeNode root = new TreeNode(nums[mid]);
root.left = helper(nums,low,mid - 1);
root.right = helper(nums,mid + 1,hight);
return root;
}
}