MediumTrees
Binary Tree Right Side View
metaamazonbloombergmicrosoftgoogleadobe
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.
Examples
Example 1
1
/ \
2 3
\ \
5 4Input
root = [1,2,3,null,5,null,4]
Output
[1,3,4]
At level 0, the rightmost node is 1. At level 1, the rightmost node is 3. At level 2, the rightmost node is 4.
Example 2
1
\
3Input
root = [1,null,3]
Output
[1,3]
At level 0, the rightmost node is 1. At level 1, the rightmost node is 3.
Example 3
Input
root = []
Output
[]
An empty tree has no visible nodes from any side.
Constraints
- The number of nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Optimal complexity
Time
O(n)
Space
O(n)
One problem, two ways to prep
Choose between solo practice and interview simulation
Practice Mode keeps things simple with code + tests. AI Interview Mode adds voice, pressure, and a post-round score summary.