SDE Path

Top View of a Binary Tree

Medium

Top View of a Binary Tree

Print the nodes visible when a binary tree is viewed from the top.

Input format

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

Output format

Print the first node of each column in BFS order, columns left→right.

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
77 62 20 83 96 93 51 57 10 39 95 N N 2 74 94 99 N 35 N N 24 N N N N N N 52 48 42
Expected output
94 57 83 62 77 20 51 74
Example 2
Input
64 62 14 39 51 47 49 22 73 71 6 N N 96 21 N 5 N N N N 12 N N N N N N N N 31
Expected output
22 39 62 64 14 49 21