# Maximum Binary Tree II: Explained

Maximum Binary Tree II shows up often in coding rounds. Here is how I talk through it when the interviewer is watching.

## Key idea

A maximum binary tree is built by taking the max as root and recursing on left/right parts. You are given such a tree from an array, and you must insert a new value val that was appended at the end of the array. Walk the right spine: val becomes the new root of the current subtree once you find a node < val, and that node becomes val's left child; otherwise attach val as the new rightmost leaf.

## Steps to say out loud

1. Restate the input and the exact ask, and nail down constraints and overflow.
2. Name the pattern (binary search on the answer, monotonic stack or deque, interval / interval-DP, string hashing, graph traversal, or shortest paths) and why a nested scan or naive search fails.
3. Code it, then trace a short example plus one empty or already-solved edge case.

## Complexity

O(n).

## Common mistake

Inserting val as if it were prepended (that would walk the left spine) or rebuilding the tree from the implicit array.

## Practice tip

Time yourself: two minutes to explain, ten to code, three to test edges. Disclosure: I work on Linkjob AI. I also keep a practice cheat sheet at Linkjob.