Binary Tree Right Side View

mediumTreesBFS taking last node of each levelSolve on LeetCode#199

Part of Practice: Levels & BST Rules

Imagine standing to the right of a binary tree and looking at it sideways: at each depth you can only see the node closest to you. Given the root, return the values of the visible nodes from top to bottom.

The tree has 0 to 100 nodes. Note that a left child can be visible if nothing at its depth sits further right.

Examples

Input:
root = [1,2,3,null,5,null,4]
Output:
[1,3,4]

The levels are [1], [2,3], [5,4]. The rightmost node of each level is 1, then 3, then 4; the 5 sits to the left of 4 at the same depth, so it is hidden.

Input:
root = [1,2]
Output:
[1,2]

2 is a left child, but nothing at its depth is further right, so it is visible.

Input:
root = []
Output:
[]

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.