Maximum Binary Tree II LeetCode solution in JavaScript

1
/**
2
* @param {TreeNode} root
3
* @param {number} val
4
* @return {TreeNode}
5
*/
6
var insertIntoMaxTree = function (root, val) {
7
// get new node
8
var node = new TreeNode(val);
9

10
// no root
11
if (!root) {
12
return node;
13
}
14

15
// upward derivation if val larger then root
16
if (val > root.val) {
17
return (node.left = root), node;
18
}
19

20
// downward derivation
21
root.right = insertIntoMaxTree(root.right, val);
22

23
// root construct
24
return root;
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0