Binary Tree Right Side View
mediumSave
Binary TreeBreadth-First SearchDepth-First SearchTree
Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Example 1
Input
[ [ 1, 2, 3, null, 5, null, 4 ] ]
Output
[1, 3, 4]
Explanation. From the right hand side, these are the visible nodes.
Example 2
Input
[ [ 1, null, 3 ] ]
Output
[1, 3]
Explanation. Only the root and the right child are visible from the right side.
Example 3
Input
[ [] ]
Output
[]
Explanation. No nodes are visible as the tree is empty.
Example 4
Input
[ [ 1, 2 ] ]
Output
[1, 2]
Explanation. The root and the left child are visible as there is no right child.
Follow-up: Can you solve this problem using both recursive and iterative approaches?
Constraints:
The number of nodes in the tree is in the range [0, 100].\n-100 <= Node value <= 100
- Views
- 1