SDE Path

Bottom View of a Binary Tree

Medium

Bottom View of a Binary Tree

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

Input format

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

Output format

Print the last 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
49 55 3 4 83 75 51 89 43 7 39 N 74 N N 68 32 37 23 N N N N N N 5 30 N N 19 N N 92 N 81 54 29 N 71 N N N N 95 N N N N N 26 60 N 6
Expected output
26 6 60 19 71 7 23 92 51
Example 2
Input
14 32 N 74 82 N N 68 3 N N 8 N 56 66 N 51
Expected output
74 56 51 66