Vertical Order Traversal of a Binary Tree

HardTrees
Next
TreeBFSSortingHash Map

Give every node a coordinate: the root is at (row 0, column 0); a left child is at (row + 1, column - 1) and a right child at (row + 1, column + 1). Report the nodes column by column from the leftmost column to the rightmost. Inside a column, order the nodes by row (top to bottom), and nodes in the same row and column by value (smaller first). Build the tree from the level-order input yourself, then solve the problem.

Input

A single line giving the tree in level order, with the word null for a missing child (for example 3 9 20 null null 15 7).

Output

One line per column from left to right, containing that column's values in the order described.

Constraints

1 <= number of nodes <= 1000 0 <= value <= 1000

Example 1
Input
3 9 20 null null 15 7
Output
9
3 15
20
7

Columns left to right: 9 / 3 15 / 20 / 7.

Hints

Vertical Order Traversal of a Binary Tree - Hard Trees Problem | Skill Shiksha