

题解
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } *//** * @param {number[]} nums * @return {TreeNode} */var constructMaximumBinaryTree = function (nums) { return build(nums, 0, nums.length - 1);};const build = (nums, low, high) => { let idx = 0; let maxValue = Number.MIN_SAFE_INTEGER; for (let i = low; i <=high; i++) { let num = nums[i]; if (num > maxValue) { maxValue = num; idx = i; } } if (low > high) return null; let newNode = new TreeNode(maxValue); newNode.left = build(nums, low, idx-1); newNode.right = build(nums, idx + 1, high); return newNode;}