SDE Path

Vertical Order Traversal

Medium

Vertical Order Traversal

Traverse a binary tree column by column from left to right.

Input format

The first line contains level-order tokens, where N denotes a null node.

Output format

Print one line per column (left→right); within a column order by depth, ties by smaller value first.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
63 N 61 97 N 32
Expected output
32
63 97
61
Example 2
Input
90 74 68 N N 66 67 91 87 51 12 64 44 3 16 46 N 41 76 N 60 N N 95 49 N N N 29 N N 17 39 N N N 4 N N 59 96 34 77 65 N N 21 N N N N N N N N N 79
Expected output
64
74 91 60 95
90 66 3 44 46 4 59
68 51 87 29 49 21
67 16 41 34 96
12 17
76 65 77
39 79