Skip to content

Commit 0326d95

Browse files
committed
Add solution #998
1 parent 595a93c commit 0326d95

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,076 LeetCode solutions in JavaScript
1+
# 1,077 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -806,6 +806,7 @@
806806
995|[Minimum Number of K Consecutive Bit Flips](./solutions/0995-minimum-number-of-k-consecutive-bit-flips.js)|Hard|
807807
996|[Number of Squareful Arrays](./solutions/0996-number-of-squareful-arrays.js)|Hard|
808808
997|[Find the Town Judge](./solutions/0997-find-the-town-judge.js)|Easy|
809+
998|[Maximum Binary Tree II](./solutions/0998-maximum-binary-tree-ii.js)|Medium|
809810
1002|[Find Common Characters](./solutions/1002-find-common-characters.js)|Easy|
810811
1004|[Max Consecutive Ones III](./solutions/1004-max-consecutive-ones-iii.js)|Medium|
811812
1005|[Maximize Sum Of Array After K Negations](./solutions/1005-maximize-sum-of-array-after-k-negations.js)|Easy|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* 998. Maximum Binary Tree II
3+
* https://leetcode.com/problems/maximum-binary-tree-ii/
4+
* Difficulty: Medium
5+
*
6+
* A maximum tree is a tree where every node has a value greater than any other value
7+
* in its subtree.
8+
*
9+
* You are given the root of a maximum binary tree and an integer val.
10+
*
11+
* Just as in the previous problem, the given tree was constructed from a list a
12+
* (root = Construct(a)) recursively with the following Construct(a) routine:
13+
* - If a is empty, return null.
14+
* - Otherwise, let a[i] be the largest element of a. Create a root node with the value a[i].
15+
* - The left child of root will be Construct([a[0], a[1], ..., a[i - 1]]).
16+
* - The right child of root will be Construct([a[i + 1], a[i + 2], ..., a[a.length - 1]]).
17+
* - Return root.
18+
*
19+
* Note that we were not given a directly, only a root node root = Construct(a).
20+
*
21+
* Suppose b is a copy of a with the value val appended to it. It is guaranteed that b has
22+
* unique values.
23+
*
24+
* Return Construct(b).
25+
*/
26+
27+
/**
28+
* Definition for a binary tree node.
29+
* function TreeNode(val, left, right) {
30+
* this.val = (val===undefined ? 0 : val)
31+
* this.left = (left===undefined ? null : left)
32+
* this.right = (right===undefined ? null : right)
33+
* }
34+
*/
35+
/**
36+
* @param {TreeNode} root
37+
* @param {number} val
38+
* @return {TreeNode}
39+
*/
40+
var insertIntoMaxTree = function(root, val) {
41+
if (!root || val > root.val) {
42+
const newRoot = createNode(val);
43+
newRoot.left = root;
44+
return newRoot;
45+
}
46+
47+
root.right = insertIntoMaxTree(root.right, val);
48+
return root;
49+
50+
function createNode(value) {
51+
return new TreeNode(value);
52+
}
53+
};

0 commit comments

Comments
 (0)