860. The Biggest-First Tree, In Preorder
Read n DISTINCT values. Build a binary tree by this rule:
* the root is the largest value in the whole array * its left child is the tree built the same way from everything to the LEFT of that value * its right child is the tree built the same way from everything to the RIGHT of it * an empty range produces no node
Print the tree in PREORDER (node, then left subtree, then right subtree), all n values on one
line separated by single spaces.
3 2 1 6 0 5 -> 6 3 2 1 5 0
1 3 2 4 -> 4 3 1 2
5 4 3 2 1 -> 5 4 3 2 1
In the first row the root is 6. To its left the largest is 3, which takes 2 and then 1 down its right spine; to its right the largest is 5, whose left child is 0.
## Why the direct construction is quadratic
Building recursively means scanning each range for its maximum: O(n) per node, so O(n^2) in
the worst case, which is reached by an already sorted array where the recursion is a single spine.
At n = 200000 that is 2 * 10^10.
## The shape the stack builds
Sweep left to right with a stack of node indices whose values DECREASE from the bottom up. When
a[i] arrives:
last = none
while the stack is non-empty and its top value < a[i]:
last = pop
left[i] = last
if the stack is non-empty:
right[ top ] = i
push i
When the sweep ends, the BOTTOM of the stack is the root.
## Why this is the same tree
Two claims, and both are short.
First, a[i]'s left child is the last index popped. Everything popped has a smaller value than
a[i] and lies to its left, so all of it belongs in a[i]'s left subtree; the last one popped is
the largest of them, because the stack decreases from the bottom up, so it is the root of that
subtree.
Second, a[i] becomes the right child of whatever remains on top. That entry is the nearest value
to the left that is LARGER than a[i], so in the recursive construction a[i] sits somewhere in
its right range, and since a[i] is the largest value seen since then, it is the root of that
range so far. Later arrivals may overwrite this, and overwriting is correct: a larger later value
takes over the right subtree and adopts the previous occupant as its own left child, which is
exactly what the first claim does for it.
Third, the bottom of the stack is the global maximum, because nothing ever pops it. That is the root.
## Printing a preorder without recursion
A recursive traversal on a spine of 200000 nodes overflows the call stack in several of the five languages. Use an explicit stack: push the root, then repeatedly pop a node, emit it, and push its RIGHT child before its LEFT so that the left comes off first.
## The comparison, and the promise of distinctness
Pop on top value < a[i], strictly. The values are promised distinct so < and <= agree here,
but writing the strict version keeps the intent clear, and with duplicates the tree would not be
well defined in the first place.
## Related problems
Problem 154 is the same sweep, keeping only which value popped which. This one keeps the links and gets a tree for the same price.
## Cost
Time O(n) to build plus O(n) to traverse. Space O(n).
Constraints - `1 ≤ n ≤ 200000` - `-10^9 ≤ a[i] ≤ 10^9`, all distinct
Input
The first line contains one integer n.
The second line contains n distinct integers a[0] ... a[n-1].
Output
Print n integers on one line, separated by single spaces: the values of the tree in preorder, that is each node followed by its whole left subtree and then its whole right subtree. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.