Coding interview problems โ€บ Trees & BSTs

๐ŸŒณ Trees & BSTs Medium

Binary Tree Right Side View

Binary tree right side view explained: take the last node of each BFS level (or a right-first DFS), tested Python code, common mistakes and a live tree visual.

Also searched as: right view of a binary tree ยท last node of every level

๐ŸŒณ

Try it live Free

Binary Search Tree & Traversals

Run level-order in the BST lab and look at the last value printed on each row: that is what you would see from the right.

Also useful: Graph Search: BFS vs DFS

Open the playground โ†’

The problem

Imagine standing to the right of a binary tree. Return the values you can see, from top to bottom: for each level, the rightmost node.

Inputroot = [1, 2, 3, null, 5, null, 4]
Output[1, 3, 4]
Inputroot = [1, 2, 3, 4]
Output[1, 3, 4]

4 is on the left but nothing on its level is to its right.

The key insight

The visible node on each level is just the last node of that level in a level order traversal. Run BFS level by level and record the final node of each round. Note that the visible node may come from a left subtree when the right side is shorter.

Brute force first: Walking down only .right pointers fails whenever the left subtree is deeper; the BFS is already optimal.

Step-by-step approach

  1. If the root is empty, return an empty list.
  2. Run a level order BFS with a queue.
  3. For each level, process exactly len(queue) nodes, pushing their children left then right.
  4. The last node popped in that round is the rightmost of the level: append its value.

Python solution

PYTHON 3tested
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val, self.left, self.right = val, left, right

from collections import deque


def right_side_view(root):
    if not root:
        return []
    view, q = [], deque([root])
    while q:
        for _ in range(len(q)):
            node = q.popleft()
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
        view.append(node.val)          # last node popped on this level
    return view

Complexity

TimeO(n)
SpaceO(w)

Every node is enqueued and dequeued once. The queue holds at most one level, so space is the maximum width w.

Common mistakes

Frequently asked questions

What is the time and space complexity of the right side view?

O(n) time since each node is processed once, and O(w) space for the BFS queue, where w is the widest level.

Can it be done with DFS?

Yes. Do a preorder walk that visits the right child before the left, passing the depth. The first time you reach a new depth, that node is the visible one.

How do I get the left side view?

Record the first node of each BFS level instead of the last, or in DFS visit the left child before the right.

Preparing for placements?

Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.

All coding interview problems More Trees & BSTs Binary Search Tree & Traversals lab Placement prep guides

Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.