← Queue / BFS

Pattern Recognition Drill

#140 — Binary Tree Right Side View

Medium Trees

The Problem

Given a binary tree, return the values visible from the right side (rightmost node at each level).

What approach would you use?

Think about it before scrolling down.

Key Signals

Queue / BFS

Level-order traversal. Append the last node of each level to result. O(n).

Alt: DFS / Recursion

DFS going right first. Track level; if level == len(result), append value. O(n).

Common Trap

DFS right-first works but BFS is more intuitive. Don't assume it's just the rightmost branch.

← #139