Binary Tree Right Side View - Problem
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.
When you stand on the right side, you can only see the rightmost node at each level of the tree.
Example: For a tree with root [3,9,20,null,null,15,7], you would see nodes with values [3,20,7] - one from each level, taking the rightmost visible node.
Input & Output
Example 1 — Standard Tree
$
Input:
root = [3,9,20,null,null,15,7]
›
Output:
[3,20,7]
💡 Note:
Level 0: only node 3 is visible. Level 1: nodes 9 and 20, but only 20 is rightmost. Level 2: nodes 15 and 7, but only 7 is rightmost.
Example 2 — Right-Skewed Tree
$
Input:
root = [1,null,3]
›
Output:
[1,3]
💡 Note:
Level 0: node 1 is visible. Level 1: only node 3 exists and is rightmost.
Example 3 — Single Node
$
Input:
root = [1]
›
Output:
[1]
💡 Note:
Only one node exists, so it's the only node visible from the right side.
Constraints
- The number of nodes in the tree is in the range [0, 100]
- -100 ≤ Node.val ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Tree
Binary tree with nodes at multiple levels
2
Right Side View
Only rightmost node at each level is visible
3
Output Array
Values of visible nodes from top to bottom
Key Takeaway
🎯 Key Insight: At each level of the tree, only the rightmost node is visible from the right side view
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code