Convert Sorted Array to Binary Search Tree
给一个sorted数组, 建立一个bst.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return rec(nums, 0, nums.length - 1);
}
public TreeNode rec (int[] nums, int start, int end) {
if(start > end)
return null;
int mid = start + (end - start) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = rec(nums,start,mid-1);
root.right = rec(nums,mid+1, end);
return root;
}
}