← DSA Atlas
Dedicated problem page · #199

Binary Tree Right Side View

MediumTrees and Binary Search TreesLevel-order BFS, take the last of each levelBreadth-first search by level
Solve on LeetCode ↗
199
MediumTrees and Binary Search TreesBreadth-first search by levelLevel-order BFS, take the last of each level

Binary Tree Right Side View

Given the root of a binary tree, imagine standing on the right side of it. Return the values of the nodes you can see, ordered from top to bottom — that is, the rightmost node at each depth level.

Open official problem prompt ↗
In plain English

Collect the rightmost node value at every depth of the tree, from the root down.

Picture it like this

Stand to the right of a bookshelf: on each shelf (level) you can only see the book at the far right end; the ones behind it are hidden.

Example
Input
root = [1,2,3,null,5,null,4]
Output
[1,3,4]
Why
Level 0 sees 1, level 1's rightmost is 3, and level 2's only node 4 (child of 3) is visible.
Constraints
The number of nodes is in the range [0, 100]-100 <= Node.val <= 100
Pattern lesson

See the pattern, then code

Level-order BFS, take the last of each level
Recognition clue

You want one value per depth level (the one furthest right), which is a level-by-level BFS where you keep the last node dequeued at each level.

Trees and Binary Search Trees

Hierarchies, subtree aggregation, path properties, or ordered tree queries.. Process the tree level by level; the last node handled within a level is exactly the one visible from the right side at that depth.

New words, made simpleKnow these before the algorithm
Level / depth
All nodes the same number of edges from the root
Level-size snapshot
Recording the queue length before processing so you know where the level ends
Width
The maximum number of nodes on any single level
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
DFS tracking first-seen per depth

Works and uses less memory on skewed trees, but the level boundary logic is easy to get subtly wrong.

Traverse right child before left and record the first value encountered at each new depth.

Time O(n)Space O(h)
The rule we keep true

Invariant

At the start of each outer loop iteration, the queue contains exactly the nodes of one complete level, in left-to-right order.

Why this is correct

Reasoning

Because children are enqueued left then right and a whole level is drained before the next begins, the last node dequeued in a level is its rightmost node — precisely what is visible from the right. Doing this per level yields one value per depth, top to bottom.

The algorithm in three movesSay these aloud before coding
1Return empty for an empty tree

level0 last -> 1

2Use a queue and process one full level per outer iteration

level1 nodes [2,3] last -> 3

3For each level, record the value of its last node

level2 nodes [5,4] last -> 4

4Enqueue children left then right so the last dequeued is the rightmost

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
10
31
42
1 · Readqueue=[1]
2 · AskWhich is the last node?
3 · Update statesize 1, i=0 is last
4 · Resultres=[1]; enqueue 2 then 3
Key takeaway

The right-side-visible nodes 1, 3, 4 read top to bottom.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 5-6Empty guard

    An empty tree has nothing visible, so return an empty list.

  2. 2
    Lines 10-11Freeze level size

    Capturing the count before the inner loop separates this level's nodes from children added during it.

  3. 3
    Lines 13-15Grab the rightmost

    The last index in the level is the node seen from the right.

  4. 4
    Lines 16-19Enqueue children

    Left before right preserves left-to-right order so 'last' really means rightmost.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • Empty tree returns an empty list
  • A left-only tree still shows one node per level (the only nodes present)
  • A single node returns just its value
  • A level whose rightmost node has no children ends that branch but other branches may extend deeper
!

Common beginner mistakes

  • Recomputing len(queue) inside the inner loop after enqueuing children, mixing levels together
  • Assuming the right child is always the visible one — if the rightmost node lacks a right child, its left child (or a deeper left branch) may be visible
  • Enqueuing right before left, which would make 'last' the leftmost node
Check your understanding

Why capture level_size before the inner loop rather than using len(queue) directly each step?