Binary Tree Right Side View
Return the values visible from the right side — the last node of each level.
Open on LeetCode ↗Intuition
Level order traversal, but keep only each level's final element. Or DFS right-first, recording the first node reached at each new depth — both express 'rightmost per level'.
Level-order with a queue is the obvious route, but DFS works too if you visit the right child first: then the first node reached at each depth is the rightmost one. Comparing depth == len(view) is a neat way to ask "is this the first time I've been this deep?" without tracking levels explicitly.
Approach
BFS take
Standard level-order; when the per-level loop ends, the last popped value is the visible one.
DFS take
Visit right before left, carrying depth. If depth == len(view), this node is the first seen at that depth → rightmost.
Pick by constraint
BFS is iterative and obvious; DFS is shorter and O(h) space on balanced trees.
Solution & live demo
Common pitfalls
Recursing left before right
dfs(node.left, depth + 1) dfs(node.right, depth + 1)
dfs(node.right, depth + 1) dfs(node.left, depth + 1)
The whole method rests on the first arrival at each depth being the rightmost node. Going left first records the left side view instead — the code looks right and the answer is silently mirrored.
Overwriting the stored value at each depth
if depth < len(view):
view[depth] = node.val
else:
view.append(node.val)if depth == len(view):
view.append(node.val)With right-first ordering the first node seen at a depth is already the correct one, so later nodes at that depth must be ignored. Overwriting replaces it with a node further left.
Tracking depth with a shared mutable counter
self.depth += 1 dfs(node.right) self.depth -= 1
dfs(node.right, depth + 1)
It's an extra pair of mutations to keep balanced across every branch and early return. Depth is naturally per-path, so it belongs in the call argument where the language unwinds it for you.
Edge cases
A left node can be visible if the right subtree is shallower — level-based logic handles it (unlike 'just walk right').
[].