-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0654-maximum-binary-tree.js
38 lines (34 loc) · 1.19 KB
/
0654-maximum-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 654. Maximum Binary Tree
* https://leetcode.com/problems/maximum-binary-tree/
* Difficulty: Medium
*
* You are given an integer array nums with no duplicates. A maximum binary tree can be built
* recursively from nums using the following algorithm:
* 1. Create a root node whose value is the maximum value in nums.
* 2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
* 3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.
*
* Return the maximum binary tree built from nums.
*/
/**
* 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) {
if (!nums.length) return null;
const max = Math.max(...nums);
const index = nums.indexOf(max);
const head = new TreeNode(max);
head.left = constructMaximumBinaryTree(nums.slice(0, index));
head.right = constructMaximumBinaryTree(nums.slice(index + 1));
return head;
};