Problem statement
Given the root of a binary tree, imagine standing on its right side. Return the values of the nodes you can see, from top to bottom — that is, the rightmost node on each level.
Trees below are written in level order; null marks a missing child.
Examples
- Input
root = [1, 2, 3, null, 5, null, 4]
- Output
[1, 3, 4]
- Input
root = [1, 2, 3, 4]
- Output
[1, 3, 4]
Why: The 4 hangs off the left branch, but nothing on its level is further right, so it is visible.
- Input
root = []
- Output
[]
Constraints
- 0 ≤ number of nodes ≤ 100
- −100 ≤ node value ≤ 100
How to answer it out loud
What it tests: Level-order traversal, turning a visual description into a precise rule, and quick edge-case testing.
- 1Clarify
Clarify that “visible” means the rightmost node on each level, and what an empty tree returns.
- 2Implement
Run a level-order BFS and record the last node processed on each level.
- 3Verify
Test a tree whose deepest visible node is on the left branch, and an empty tree; explain O(n) time.
From brute force to the best solution
| Approach | Idea | Time | Space |
|---|---|---|---|
| Level-order BFSBest | Process the tree one level at a time and keep the last node of each level. | O(n) | O(width) |
| Right-first DFS | Visit right before left; the first node reached at each new depth is visible. | O(n) | O(height) |
“Rightmost on its level” is not the same as “reachable by always going right”. Processing whole levels makes the definition exact.
Reference solution
Each version is self-contained and has been run against the examples and test cases on this page.
from collections import dequefrom typing import Optionalclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val, self.left, self.right = val, left, rightdef right_side_view(root: Optional[TreeNode]) -> list[int]: if not root: return [] view, queue = [], deque([root]) while queue: level_size = len(queue) for i in range(level_size): node = queue.popleft() if i == level_size - 1: # last node on this level is the visible one view.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) return view- Time
- O(n)
- Space
- O(width)
- Approach
- Level-order BFS
Edge cases to run before you say “done”
| Input | Expected | Why it matters |
|---|---|---|
| [1, 2, 3, 4] | [1, 3, 4] | Breaks “just follow right pointers”. |
| [] | [] | Empty tree. |
| [1, 2] | [1, 2] | A left-only child is visible. |
Answer the main question first, then take these on one at a time:
- Can you solve it with DFS instead?
- How would you return the left side view?
- What would the view from the top look like?
Check your recording or written solution against this list:
- Only following right pointers
- Mixing nodes from different levels
- Not handling an empty tree
This guide combines general interview-practice patterns with the public hiring material below. The practice prompt and coaching are PiriPiri AI editorial content, not official Meta questions or answers.
Official sources reviewed 20 September 2026.