/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param num int整型一维数组
* @return TreeNode类
*/
public TreeNode sortedArrayToBST (int[] num) {
// write code here
if(num == null ||num.length == 0){
return null;
}
//选择区间中点作为根节点
return createBST(num, 0 , num.length - 1);
}
private TreeNode createBST(int[] num, int left, int right){
//递归终止
if(left >right) return null;
int mid = (right - left + 1) / 2 +left;
TreeNode root = new TreeNode(num[mid]);
root.left = createBST(num, left,mid - 1);
root.right = createBST(num, mid +1,right);
return root;
}
}