Meta · Coding 2 of 3

Binary Tree Right Side View

Return the values you can see from the right side of a binary tree.

MediumTreeBFSBest: O(n) time · O(width) space

Practice question: an editorial practice prompt, not a claim that Meta asks this exact question. Interviews vary by role, level, team, and location.

Problem

Problem statement

Given the root of a binary tree, imagine standing on its right side. Return the values of the nodes you can see, from top to bottom — that is, the rightmost node on each level.

Trees below are written in level order; null marks a missing child.

Examples

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

Why: The 4 hangs off the left branch, but nothing on its level is further right, so it is visible.

Example 3
Input
root = []
Output
[]

Constraints

  • 0 ≤ number of nodes ≤ 100
  • −100 ≤ node value ≤ 100
In the interview

How to answer it out loud

What it tests: Level-order traversal, turning a visual description into a precise rule, and quick edge-case testing.

  1. 1
    Clarify

    Clarify that “visible” means the rightmost node on each level, and what an empty tree returns.

  2. 2
    Implement

    Run a level-order BFS and record the last node processed on each level.

  3. 3
    Verify

    Test a tree whose deepest visible node is on the left branch, and an empty tree; explain O(n) time.

Approaches

From brute force to the best solution

ApproachIdeaTimeSpace
Level-order BFSBestProcess the tree one level at a time and keep the last node of each level.O(n)O(width)
Right-first DFSVisit right before left; the first node reached at each new depth is visible.O(n)O(height)
Key insight

“Rightmost on its level” is not the same as “reachable by always going right”. Processing whole levels makes the definition exact.

Solution

Reference solution

Each version is self-contained and has been run against the examples and test cases on this page.

from collections import dequefrom typing import Optionalclass TreeNode:    def __init__(self, val=0, left=None, right=None):        self.val, self.left, self.right = val, left, rightdef right_side_view(root: Optional[TreeNode]) -> list[int]:    if not root:        return []    view, queue = [], deque([root])    while queue:        level_size = len(queue)        for i in range(level_size):            node = queue.popleft()            if i == level_size - 1:     # last node on this level is the visible one                view.append(node.val)            if node.left:                queue.append(node.left)            if node.right:                queue.append(node.right)    return view
Time
O(n)
Space
O(width)
Approach
Level-order BFS
Test cases

Edge cases to run before you say “done”

InputExpectedWhy it matters
[1, 2, 3, 4][1, 3, 4]Breaks “just follow right pointers”.
[][]Empty tree.
[1, 2][1, 2]A left-only child is visible.
Likely follow-ups

Answer the main question first, then take these on one at a time:

  • Can you solve it with DFS instead?
  • How would you return the left side view?
  • What would the view from the top look like?
Avoid these mistakes

Check your recording or written solution against this list:

  • Only following right pointers
  • Mixing nodes from different levels
  • Not handling an empty tree
Sources and methodology

This guide combines general interview-practice patterns with the public hiring material below. The practice prompt and coaching are PiriPiri AI editorial content, not official Meta questions or answers.

Official sources reviewed 20 September 2026.