Binary Tree Right Side View asks what you would see standing to the right of a tree: one node per level, the rightmost one. It is a clean excuse to practice breadth-first search level by level — a workhorse pattern for tree problems.
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 top to bottom.
Example: tree 1 -> (2, 3), with 2 -> (None, 5) and 3 -> (None, 4) gives [1, 3, 4] — the rightmost
node on each level.
The slow way first
A first instinct is to recurse and somehow track depth and horizontal position, then sift out the rightmost node per depth at the end. That works, but the bookkeeping is fiddly and easy to get wrong. The cleaner mental model is to walk the tree one full level at a time and simply grab the last node of each level.
The question to ask: which node on a level is visible from the right? The last one, when the level is read left to right. If we process levels in order, the answer falls out for free.
The idea: BFS, keep the last of each level
Use breadth-first search. Hold the current level as a list of nodes. Read it left to right; the last node is the one visible from the right, so append its value to the answer. While scanning, collect the next level (each node's left child then right child). Repeat until there are no more nodes.
The key insight: because we read each level left to right, the last node we touch is automatically the rightmost — no coordinates or depth math needed.
Walk through it
Step through the animation. Each level lights up as we scan it. When the level is fully scanned, its rightmost node turns green and its value joins view. Level 0 gives 1, level 1 gives 3 (2 is hidden behind it), level 2 gives 4 (5 is hidden). The result is [1, 3, 4].
Pseudocode
if the tree is empty: return []
view = []
queue = [root]
while queue is not empty:
level = queue # this whole level
queue = [] # start collecting the next level
for i, node in level (left to right):
if node is the last in level:
append node.val to view
push node.left, then node.right onto queue (if they exist)
return viewThe Python solution
def right_side_view(root):
if not root:
return []
view, queue = [], [root]
while queue:
level = queue
queue = []
for i, node in enumerate(level):
if i == len(level) - 1:
view.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
return viewqueueholds the nodes of the current level; we swap it intoleveland start a freshqueuefor the next one.enumerate(level)reads the level left to right, giving us the indexi.i == len(level) - 1is true only for the last node of the level — that is the rightmost one we can see.- We push
node.leftthennode.right, so the next level is also built left to right, keeping the invariant. - When
queueempties, every level has been processed andviewis the answer.
Complexity
| Case | Time | Notes |
|---|---|---|
| BFS (this solution) | O(n) (moderate) | each node visited once |
O(n) (moderate)We touch every node exactly once, so time is O(n). The extra space is the queue, which in the worst case holds the widest level — up to O(n) for a full tree.
When this pattern shows up
Whenever a problem says "per level", "level order", "closest to the root", or "shortest path in an unweighted graph," reach for BFS with a queue. Processing a whole level before the next is the move behind level-order traversal, right/left side views, and minimum-depth.
Build the next level in left-to-right order (left child before right child). If you push right first, the last node you read is no longer the rightmost, and the answer flips.
Practice
If node 3 had no children but node 2 had a right child 5, what would level 2 be, and what would the right side view pick from it?
1. Why is the last node of each level the one visible from the right?
2. Why must children be pushed left before right?
3. What is the time complexity?
4. For the example tree, what is the right side view?