Binary Tree Right Side View
MediumP1Coding
Imagine standing on the right side of a binary tree. Given its root, return the values of the nodes you can see, ordered from top to bottom. At each depth you see the rightmost node of that level.
Tree nodes have val, left and right. The class TreeNode is already defined for you. The tree arrives in level-order form (as in the examples) and you receive its root, which is null for an empty tree.
Example 1
- Input
- root = [1,2,3,null,5,null,4]
- Output
- [1,3,4]
Example 2
- Input
- root = [1,2,3,4]
- Output
- [1,3,4]
- Why
- Level 3 has only node 4, which is visible because nothing is to its right.
Example 3
- Input
- root = []
- Output
- []
Constraints
- The number of nodes is in the range [0, 100].
- -100 <= Node.val <= 100
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.