What You See Looking at the Tree From the Right

Implement rightEdgeView

You are given the root of a binary tree. Picture yourself standing on its right-hand side, looking towards the tree. At every level you can see only the rightmost node of that level; anything to its left is hidden. Return the values of the visible nodes ordered from the top level to the bottom level.

You could gather all the values of each level and keep the last one, or walk level by level with a queue and note only the last node of every level.

Example 1:

Input: root = [12,5,18,3,8,null,21,1]

Output: [12,18,21,1]

Example 2:

Input: root = [7,3]

Output: [7,3]

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
  • ●Imagine standing to the right of the tree. At each level you can see only the rightmost node of that level (nodes to its left are hidden behind it). Return those visible values from the top level to the bottom. An empty tree gives an empty list

root =

[12, 5, 18, 3, 8, null, 21, 1]