MediumTreeDepth-First SearchBreadth-First SearchBinary TreeUpdated Sep 2026

199. Binary Tree Right Side View

Asked at TikTok

Difficulty

Medium

Time

O(n)

Space

O(w)

Pattern

Tree

Companies

1

Problem

You are given the root of a binary tree. Imagine you stand far to the right of the tree and look at it. Some nodes are hidden behind other nodes. Return the values of the nodes you can see, ordered from the top level down to the bottom level.

On each level of the tree, exactly one node is visible from the right: the rightmost node of that level. So the answer has one value per level, and the answer length equals the height of the tree.

Be careful: the rightmost node of a level is not always in the right subtree of the root. If the right subtree is short, the visible node at deeper levels comes from the left subtree. If the tree is empty, return an empty list.

What you see from the right

251level 0 → 13level 1 → 34level 2 → 4The green nodes are the ones not blocked by any nodeto their right.
Looking from the right, you see one node per level: the rightmost one. Here that is 1, then 3, then 4.

Constraints

  • ●The number of nodes in the tree is in the range [0, 100].
  • ●-100 <= Node.val <= 100

Examples

Example 1
Inputroot = [1,2,3,null,5,null,4]
Output[1,3,4]
WhyLevel 0 holds only node 1, so 1 is visible. Level 1 holds nodes 2 and 3; node 3 is the rightmost, so 3 is visible. Level 2 holds nodes 5 and 4; node 4 is the rightmost, so 4 is visible. The result is [1,3,4].
Example 2
Inputroot = [1,2,3,4,null,null,null,5]
Output[1,3,4,5]
WhyLevel 0 gives 1. Level 1 gives the rightmost node 3. Level 2 has only node 4 (the right child of 2 is null), so 4 is visible. Level 3 has only node 5, the left child of 4, so 5 is visible. Node 5 sits in the left subtree but nothing blocks it, so the result is [1,3,4,5].
Example 3
Inputroot = [1,null,3]
Output[1,3]
WhyLevel 0 gives 1. Level 1 has only node 3, so it is the rightmost node of that level. The result is [1,3].
Example 4
Inputroot = []
Output[]
WhyThere are no nodes, so nothing can be seen. The result is an empty list.

Asked At

1 companies
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.

BFS level by level

queue1frontbackStart: the queue holds only the root, level 0.queue23frontbackLevel 1: pop 2 then 3, and push their children. Thelast popped node, 3, is visible.queue54frontbackLevel 2: pop 5 then 4. The last popped node, 4, isvisible. The queue is now empty.size = len(queue) before the levellast node popped on the level → append its valueanswer = [1, 3, 4]
Process one level at a time. The last node taken out of the queue on that level is the visible one.
ComplexityTime O(n)Space O(w)Each node enters and leaves the queue once, and the queue holds at most one level, so its size is bounded by the tree's maximum width w.

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

O(n) time
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 runs in O(n) time and O(w) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

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.