构建二叉搜索树
/* 利用二叉搜索树的特点:根节点是中间的数 每次找到中间数,左右子树递归子数组 */ public TreeNode sortedArrayToBST(int[] nums) { return builder(nums,0,nums.length-1); } public TreeNode builder(int[] nums,int left,int right) { if (left>right) return null; int mid = (left+right)/2; TreeNode temp = new TreeNode(nums[mid]); temp.left = builder(nums,left,mid-1); temp.right = builder(nums,mid+1,right); return temp; }