mediumTreeBFSTrees

Binary Tree Right Side View

mediumTime: O(n)Space: O(n)

Signals to notice

rightmost node at each levellevel order with only last elementBFS keeping track

Brute force first

Full level order traversal, take last element of each level — but stores all nodes per level unnecessarily. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

BFS: at each level, only record the last node processed. You already know the level size from the queue — the last node in each batch is the right side view. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

What must stay true

The right side view is exactly the last node at each depth level. BFS with level-size tracking naturally identifies this node. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Easy way to go wrong

Thinking you need DFS right-first traversal — that works too, but BFS is more intuitive. With DFS, you must visit the right child first and only record the first node seen at each new depth. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Trees Pattern