class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
//二叉搜索树(又称二叉排序树) 的中序遍历 可得到一个升序序列,又平衡二叉树 中所有节点的左右子树的高度差绝对值不超过1.
//根据这个升序序列构建平衡二叉树,根节点为序列的中心元素,(仅由中序序列不能唯一确定一棵树)
return buildTree(nums,0,nums.length-1);
}
private TreeNode buildTree(int[] nums,int low,int high){
if(low > high) return null;
//根据序列长,得到序列的中心节点下标
int mid = low + (high - low)/2;
TreeNode root = new TreeNode(nums[mid]);
//根据根节点构建左右子树
root.left = buildTree(nums,low,mid-1);
root.right = buildTree(nums,mid+1,high);
//完成
return root;
}
}