What You See Looking Down at the Tree

Implement topEdgeView

You are given the root of a binary tree. Give each node a column: the root is in column 0, a left child is one column to the left of its parent and a right child one column to the right. Looking down at the tree from above, in each column you see only the node nearest to the top; if two nodes are equally high in a column, you see the one that comes first in level order. Return the visible values from the leftmost column to the rightmost.

A breadth-first walk hands you nodes in top-to-bottom order, so the first node to reach a column is the one you would see.

Example 1:

Input: root = [10,5,12,3,8,11,15,1,null,7]

Output: [1,3,5,10,12,15]

Example 2:

Input: root = [4,2,9]

Output: [2,4,9]

Example 3:

Input: root = []

Output: []

+ 11 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes ≤ 100; −100 ≤ node.val ≤ 100
  • ●The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child
  • ●Give each node a column: the root is column 0, a left child is one column to the LEFT of its parent (column − 1), a right child one column to the RIGHT (column + 1). Each node also has a row (its depth)
  • ●Looking down from above, in each column you see only the node closest to the top (smallest row). If two nodes tie for that (same row, same column), the one that comes first in level order (the more leftward) is seen. Return the visible values from the leftmost column to the rightmost. An empty tree gives an empty list

root =

[10, 5, 12, 3, 8, 11, 15, 1, null, 7]