Binary Tree Right Side View
MediumTrees
Given the root of a binary tree, imagine standing to its right and looking at it. At each depth you can see only the rightmost node.
Return the values of the visible nodes ordered from the top level to the bottom level. The visible node at a depth is the rightmost node at that depth, which may come from the left subtree when the right side is shorter. Return an empty list for an empty tree.
Trees are written in level order: values are listed level by level, left to right, and null marks a missing child (trailing nulls are omitted).
Example 1
- Input:
- root = [10,5,15,null,7,12,null,6,null,null,13]
- Output:
- [10, 15, 12, 13]
- Explanation
- At each depth the rightmost node wins: 10, then 15, then 12 (rightmost of 7 and 12), then 13 (rightmost of 6 and 13). 12 is a left child, so the visible node does not have to be a right child.
Example 2
- Input:
- root = [1,2,null,3]
- Output:
- [1, 2, 3]
- Explanation
- Every node is the only one at its depth, so all of them are visible even though the tree leans left.
Example 3
- Input:
- root = [1,2,3,4]
- Output:
- [1, 3, 4]
- Explanation
- The right subtree stops at depth 2, so at depth 3 the only node is 4 from the left subtree, and it is visible.
Constraints
- The number of nodes is in the range
[0, 100] -100 <= Node.val <= 100