Watch 10 video solutions for Maximum Binary Tree II, a medium level problem involving Tree, Binary Tree. This walkthrough by Muerde un zombie has 363 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A maximum tree is a tree where every node has a value greater than any other value in its subtree.
You are given the root of a maximum binary tree and an integer val.
Just as in the previous problem, the given tree was constructed from a list a (root = Construct(a)) recursively with the following Construct(a) routine:
a is empty, return null.a[i] be the largest element of a. Create a root node with the value a[i].root will be Construct([a[0], a[1], ..., a[i - 1]]).root will be Construct([a[i + 1], a[i + 2], ..., a[a.length - 1]]).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:
Input: root = [4,1,3,null,null,2], val = 5 Output: [5,4,null,1,3,null,null,2] Explanation: a = [1,4,2,3], b = [1,4,2,3,5]
Example 2:
Input: root = [5,2,4,null,1], val = 3 Output: [5,2,4,null,1,null,3] Explanation: a = [2,1,5,4], b = [2,1,5,4,3]
Example 3:
Input: root = [5,2,3,null,1], val = 4 Output: [5,2,4,null,1,3] Explanation: a = [2,1,5,3], b = [2,1,5,3,4]
Constraints:
[1, 100].1 <= Node.val <= 1001 <= val <= 100Problem Overview: You are given the root of a maximum binary tree built from an array and a new value val appended to that array. The task is to update the tree so it represents the maximum binary tree of the new array after the insertion.
A maximum binary tree has two rules: the root is the maximum value in the array, and the left and right subtrees are built recursively from the elements on each side of that maximum. When a new value is appended, it can only affect nodes along the rightmost path of the tree.
Approach 1: Recursive Tree Modification (O(n) time, O(h) space)
This method directly simulates how the tree would change if the new value were appended to the array. Compare val with the current node. If val is greater than the current root, the new value becomes the new root and the previous tree becomes its left child. Otherwise, the insertion must occur somewhere in the right subtree because the value was appended to the end of the array. Recursively process root.right until the correct position is found. The recursion depth equals the tree height h, giving O(n) worst-case time and O(h) space due to the call stack. This approach is clean and closely matches the recursive definition of a tree.
Approach 2: Iterative Approach using Stack (O(n) time, O(n) space)
The iterative solution leverages a monotonic structure similar to how the original maximum binary tree can be built. Traverse the right spine of the tree using a stack to track nodes. While the stack top has a value smaller than val, pop nodes because the new value must become their parent. The last popped node becomes the left child of the new node. If a larger node remains on the stack, attach the new node as its right child. This approach avoids recursion and works well when you want explicit control over traversal. Time complexity is O(n) since each node is pushed and popped at most once, and auxiliary space is O(n). The logic relies on properties of binary tree structure and a monotonic stack.
Recommended for interviews: The recursive modification approach is typically expected because it directly uses the definition of a maximum binary tree and quickly leads to a clean solution. Interviewers often look for the key observation that the insertion only affects the rightmost path. The iterative stack version demonstrates deeper understanding of tree construction patterns and monotonic stacks, which can stand out in more advanced interviews.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Tree Modification | O(n) | O(h) | Best general solution. Clean logic and closely follows the definition of a maximum binary tree. |
| Iterative Stack Approach | O(n) | O(n) | Useful when avoiding recursion or when applying monotonic stack patterns. |