给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums 按 严格递增 顺序排列
var sortedArrayToBST = function(nums) {if(nums.length ==0 ){return null}let mid = Math.floor(nums.length/2)let node = new TreeNode(nums[mid])let left = nums.slice(0,mid)let right = nums.slice(mid+1,nums.length)node.left = sortedArrayToBST(left)node.right = sortedArrayToBST(right)return node};
var sortedArrayToBST = function(nums) {if(nums.length ==0 ){return null}return order(nums,0,nums.length-1)};
var order = function (nums,left,right){if (left > right) {return null;}let mid = Math.floor((left+right)/2)let node = new TreeNode(nums[mid]);node.left = order(nums,left,mid-1)node.right = order(nums,mid+1,right)return node
}