AlgoMaster Logo

Binary Tree Right Side View

Last Updated: March 29, 2026

medium
3 min read

Understanding the Problem

We are looking at a binary tree from the right side. For each level of the tree, we want the rightmost visible node. That does not always mean "the right child." If a level has nodes only in the left subtree, the leftmost node at that level is still the one you would see from the right, because nothing blocks it.

So the real question is: for every depth level in the tree, what is the last node when we scan left to right? This reframes the problem from a spatial "standing on the right" visualization to a concrete algorithmic task: find the last node at each level.

This connects directly to level-order traversal. If we process each level left to right, the last node we process at each level is our answer. But we can also solve this with DFS by visiting the right subtree first.

Key Constraints:

  • Number of nodes in range [0, 100] — Extremely small input. Even an O(n^2) approach would finish instantly. The focus here is on choosing the right traversal strategy, not on optimizing for performance.
  • -100 <= Node.val <= 100 — Node values can be negative. This does not affect our traversal logic.
  • The tree can be empty (0 nodes), so we need to handle the null root case.

Approach 1: BFS (Level Order Traversal)

Intuition

The most natural way to think about this problem is level by level. If we process all nodes at each level from left to right, the last node we see at each level is the one visible from the right side. This is exactly what BFS does.

We use a queue to process nodes level by level. At each level, we dequeue all the current nodes, and whatever node we dequeue last is the right side view for that level. We just need to keep track of the last value processed in each level's iteration.

Algorithm

  1. If the root is null, return an empty list.
  2. Create a queue and add the root.
  3. While the queue is not empty:
    • Record the current queue size as levelSize.
    • Process exactly levelSize nodes from the queue.
    • For each node, enqueue its left and right children (if they exist).
    • The last node processed in this level is the right side view node. Add its value to the result.
  4. Return the result list.

Example Walkthrough

1Start BFS: queue = [1]
1queue2534
1/7

Code

BFS is already O(n) in time, which is optimal. But the queue holds an entire level at once, which can be up to n/2 nodes for a wide tree. What if we used DFS instead, where the only extra space is the recursion stack (proportional to tree height, not width)?

Approach 2: DFS (Right-First Traversal)

Intuition

Instead of processing level by level and picking the last node, what if we always visited the right subtree first? If we do a depth-first traversal where we go right before left, then the first node we encounter at each new depth level is guaranteed to be the rightmost node at that level.

The trick is knowing when we have reached a "new" depth. We can track this by comparing the current depth against the size of our result list. If depth == result.size(), we have not recorded any node at this depth yet, so the current node is the first (rightmost) one we have seen at this level.

This is elegant because it flips the perspective. Instead of scanning each level and keeping the last node, we visit in an order that naturally hits the rightmost node first.

Algorithm

  1. If the root is null, return an empty list.
  2. Create an empty result list.
  3. Define a recursive function dfs(node, depth):
    • If node is null, return.
    • If depth equals the size of result, this is the first node we have seen at this depth. Add node.val to the result.
    • Recurse on the right child with depth + 1 (right first!).
    • Recurse on the left child with depth + 1.
  4. Call dfs(root, 0) and return the result.

Example Walkthrough

1dfs(1, depth=0): new depth! Add 1, result = [1]
1add to result2534
1/6

Code