Convert an ordered array in ascending order into a highly balanced binary search tree.
In this question, a highly balanced binary tree means that the absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums==null||nums.length==0){
return null;
}
return rever(nums,0,nums.length-1);
}
public TreeNode rever(int[] nums,int left,int right){
if(left>right){
return null;
}
int mid = (right+left)/2;
TreeNode root = new TreeNode(nums[mid]);
root.left=rever(nums,left,mid-1);
root.right=rever(nums,mid+1,right);
return root;
}
}