📖 4 min read
Last updated on

Binary Tree Right Side View — Coding Interview Walkthrough


Hero Image for Binary Tree Right Side View — Coding Interview Walkthrough

You’re in a coding interview. The interviewer says: “Imagine standing to the right of a binary tree — which nodes can you see?” You’ve seen level-order traversal before. You know BFS. But now you need to isolate exactly one node per level, explain which one, and handle the case where a left subtree is deeper than the right. That’s Binary Tree Right Side View, and clarity of thought is what separates a clean solution from a fumbling one.


The Problem

Given the root of a binary tree, imagine yourself standing on the right side of it. Return the values of the nodes you can see, ordered from top to bottom.

(LeetCode #199)

Example 1

Input root = [1,2,3,null,5,null,4]

Output [1, 3, 4]

Binary tree [1,2,3,null,5,null,4] with nodes 1, 3, and 4 highlighted as the right side view.

Example 2

Input root = [1,null,3]

Output [1, 3]

Example 3

Input root = []

Output []

Already comfortable with the solution? Practice it in a mock interview →


What Interviewers Are Actually Testing

SkillWhat Interviewers Observe
BFS level traversalDo you process each level as a group and take the last node?
DFS alternativeDo you know the DFS approach (right-first, track depth)?
Edge casesNull root, single node, left-skewed tree where left nodes are visible?
ClarityDo you explain “visible = last node at each level” cleanly?

Step 1: Clarifying Questions

1. Is it always the rightmost node at each level? Yes — but “rightmost” means the last node at that depth when traversing left-to-right. If the right subtree is shorter, a left-subtree node at a deeper level is still visible.

2. Can the root be null? Yes — return [].


Step 2: Approaches

BFS (most intuitive): Level-order traversal. For each level, record the last node’s value.

DFS (right-first): DFS visiting right before left. Add a node’s value to the result when visiting a new depth for the first time.

Both are O(n) time and space.


Step 3: The Key Insight

The right side view is exactly the last node at each level of BFS. Process the tree level by level; at the end of each level, the last node processed is the one visible from the right.


Python Implementation

BFS:

from collections import deque

def rightSideView(root) -> list[int]:
    if not root:
        return []

    result = []
    queue = deque([root])

    while queue:
        level_size = len(queue)
        for i in range(level_size):
            node = queue.popleft()
            if i == level_size - 1:
                result.append(node.val)
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)

    return result

DFS (right subtree first):

def rightSideView_dfs(root) -> list[int]:
    result = []

    def dfs(node, depth: int) -> None:
        if not node:
            return
        if depth == len(result):
            result.append(node.val)  # first node seen at this depth = rightmost
        dfs(node.right, depth + 1)
        dfs(node.left, depth + 1)

    dfs(root, 0)
    return result

Time & Space Complexity

TimeSpace
BFSO(n)O(n) — queue holds one full level
DFSO(n)O(H) — recursion stack

Common Interview Mistakes

  1. Only considering the right child. A left subtree node is visible if it exists at a depth where the right subtree has no node. The BFS approach handles this automatically — whatever is last in the level queue is visible.

  2. Not grouping by level in BFS. Enqueuing all nodes without level markers means you can’t tell when a level ends. Capture len(queue) at the start of each iteration to know how many nodes belong to the current level.

  3. Null root. Return [] immediately — don’t proceed to BFS.


What a Strong Candidate Sounds Like

“I’ll use BFS level-order traversal. For each level, I take the size of the queue before processing, iterate through exactly that many nodes, and record the last one. That last node at each level is the one visible from the right. An alternative is DFS visiting right-first — the first node seen at each depth is the rightmost. Both are O(n).”


Example Interview Dialogue

Interviewer: What if the tree is left-skewed?

Candidate: Then the rightmost visible node at the deepest levels is actually a left-subtree node. BFS handles this naturally — if the right subtree is empty at some depth, whatever is last in the queue at that level (from the left subtree) gets recorded. Same for DFS: the right child is null, so we fall through to the left child and it becomes the first (only) node seen at that depth.



Practice This in a Mock Interview

Binary Tree Right Side View is a BFS fluency check. The key phrase to get right: “last node at each BFS level.” If you can say that immediately and implement it cleanly, the problem is solved.

Start a mock interview for Binary Tree Right Side View on Intervu.

Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →