Maximum Binary Tree II
We are given the root
node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.
Just as in the previous problem, the given tree was constructed from an list A
(root = Construct(A)
) recursively with the following Construct(A)
routine:
If
A
is empty, returnnull
.Otherwise, let
A[i]
be the largest element ofA
. Create aroot
node with valueA[i]
.The left child of
root
will beConstruct([A[0], A[1], ..., A[i-1]])
The right child of
root
will beConstruct([A[i+1], A[i+2], ..., A[A.length - 1]])
Return
root
.
Note that we were not given A directly, only a root node root = Construct(A)
.
Suppose B
is a copy of A
with the value val
appended to it. It is guaranteed that B
has unique values.
Return Construct(B)
.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= B.length <= 100
Last updated