Vertical Order Traversal
Hardjavapythonccppjavascript
Group the nodes of a binary tree by their horizontal distance from the root (root at distance 0, each left step -1, each right step +1). Within a column, list nodes in breadth-first order.
Input Format
The first line contains an integer M, the number of level-order tokens. The second line contains M space-separated tokens (integers or null).
Output Format
Print one line per column, from leftmost to rightmost, values separated by single spaces.
Example 1
Input
7 3 9 20 null null 15 7
Output
9 3 15 20 7
Explanation: Nodes are grouped by their horizontal distance from the root.
- 1 <= number of nodes <= 5000
- -1000000000 <= node value <= 1000000000