Medium
TreeDepth-First SearchBreadth-First SearchBinary Tree
Updated Sep 2026

Binary Tree Right Side View

Asked at TikTok

Problem

Binary Tree Right Side View asks for the values you would see standing to the right of a binary tree, top to bottom — in other words, the last node on each level. It is a level-order traversal question with a small twist, and a common follow-up after Binary Tree Level Order Traversal.

Asked At

CompanyDifficulty
TikTokMediumView all TikTok questions →

How to Think About It

1.

The rightmost node on a level is not always in the right subtree. If the right subtree is shallow, the view at deeper levels comes from the left subtree.

2.

BFS approach: process the tree level by level. For each level, the last node dequeued is the one visible from the right.

3.

DFS approach: visit right child before left child and carry the depth. The first time you reach a new depth (depth == len(result)), that node is the rightmost one at that depth.

4.

Walkthrough for [1,2,3,null,5,null,4]: level 0 -> [1] sees 1; level 1 -> [2,3] sees 3; level 2 -> [5,4] sees 4. Result [1,3,4].

5.

Edge cases: empty tree returns []; a left-only chain returns every node.

Optimal Approach

Step 1: If root is null, return [].
Step 2: Put root in a queue.
Step 3: While the queue is not empty:
size = len(queue)
Pop size nodes; push their non-null children left then right.
The last popped node of this level is visible — append its value.
Step 4: Return the result.

Every node is enqueued and dequeued once.

Time: O(n). Space: O(w) where w is the maximum width of the tree.

What Trips People Up in Real Interviews

1.

Only walking right pointers from the root. That misses nodes that are visible because the right subtree is shorter than the left.

2.

Not snapshotting the queue size before the inner loop. Without it you mix levels and cannot tell which node is last on a level.

3.

In the DFS version, visiting left before right. Then the first node you see at each depth is the leftmost, giving the left side view.

4.

Forgetting the empty-tree case and dereferencing a null root.

Solution Code

from collections import deque

def rightSideView(root):
    if not root:
        return []
    res = []
    q = deque([root])
    while q:
        size = len(q)
        for i in range(size):
            node = q.popleft()
            if i == size - 1:
                res.append(node.val)
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
    return res

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Binary Tree Right Side View problem?

Binary Tree Right Side View asks for the values you would see standing to the right of a binary tree, top to bottom — in other words, the last node on each level. It is a level-order traversal question with a small twist, and a common follow-up after Binary Tree Level Order Traversal.

How do you solve Binary Tree Right Side View?

The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

What companies ask Binary Tree Right Side View?

Binary Tree Right Side View is asked at TikTok. It is a medium difficulty problem.

What are common mistakes on Binary Tree Right Side View?
  • Only walking right pointers from the root. That misses nodes that are visible because the right subtree is shorter than the left.
  • Not snapshotting the queue size before the inner loop. Without it you mix levels and cannot tell which node is last on a level.
  • In the DFS version, visiting left before right. Then the first node you see at each depth is the leftmost, giving the left side view.
  • Forgetting the empty-tree case and dereferencing a null root.