根据中序遍历建树 不唯一
//根据中序遍历序列创建二叉搜索树
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0) return null;
return helper(nums, 0, nums.length - 1);
}
public TreeNode helper(int[] nums, int l, int r) {
if(l > r) return null;
int mid = l + (r - l) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = helper(nums, l, mid - 1);
root.right = helper(nums, mid + 1, r);
return root;
}
来源:CSDN
作者:sugargx
链接:https://blog.csdn.net/gx17864373822/article/details/104779409