Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Binary Tree Right Side View

Binary Tree Right Side View

List the nodes you would see looking at a binary tree from its right side, top to bottom. A level-order traversal problem where the answer is the last node of each level. Solve it below in Python or JavaScript, then read the walkthrough.

Solve it in your browserRead the solution
MediumTreeDepth-First SearchBreadth-First SearchBinary TreeAsked at 93 companies incl. Google, Amazon, Meta

Problem

You're given the root of a binary tree. Picture standing to the right of the tree and looking left: on each level you can see only one node, the one furthest to the right.

Return the values of those visible nodes, ordered from the top level to the bottom. A node on a deeper level can be visible even if it hangs off a left branch, as long as nothing on its level is further right.

The tree is given in level order, with null marking a missing child. An empty tree has no visible nodes.

Example 1

Input
root = [7, 3, 9, null, 4, null, 11]
Output
[7, 9, 11]

Level 0 is just 7. Level 1 is 3 and 9, and 9 is on the right. Level 2 is 4 and 11, and 11 is on the right.

Example 2

Input
root = [7, 3, 9, 2]
Output
[7, 9, 2]

The deepest level only has 2, which hangs off the left side, but nothing blocks it.

Example 3

Input
root = []
Output
[]

Constraints

  • The number of nodes is in the range [0, 1000].
  • -1000 ≤ Node.val ≤ 1000
  • Values may repeat.

Practice

1
2
3
4
5
6
7
8
9
10

Runs 13 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Binary Tree Right Side View in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Binary Tree Right Side View

Pattern: Level-order traversal (BFS by level)

The tempting shortcut is "keep walking right from the root", but that misses nodes like the 2 in [7, 3, 9, 2], which sit on a left branch once the right side runs out.

The real definition is per level: for each depth, which node is furthest right? A breadth-first search that processes one level at a time gives you each level as a group, and the last node in that group is the answer for that depth.

A depth-first search works too: visit the right child before the left, and the first node you reach at each new depth is the visible one.

Approach 1: Collect every level, then take the last

Run a level-order traversal that builds a full list of values per level, then take the last element of each list. Clear and correct, but it stores the whole tree as lists before using one value from each.

Time O(n) · Space O(n)

Approach 2: Right-first DFS

Recurse with the current depth, visiting the right child before the left. When depth == len(result), this is the first node seen at that depth, which is the rightmost, so append it. Short, but recursion depth equals tree height, which can be large on a skewed tree.

Time O(n) · Space O(h)

Approach 3: BFS keeping the last node per level

  1. Start a queue with the root (return [] for an empty tree).
  2. For each level, record the queue length, pop that many nodes, and push their children left then right.
  3. The last node popped in each level is the visible one; append its value.

Time O(n) · Space O(w), the widest level

ApproachTimeSpace
Collect every level, then take the lastO(n)O(n)
Right-first DFSO(n)O(h)
BFS keeping the last node per levelO(n)O(w), the widest level

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Binary Tree Right Side View solution in Python
from collections import deque


def right_side_view(root: Optional[TreeNode]) -> list[int]:
    if not root:
        return []
    view = []
    queue = deque([root])
    while queue:
        for _ in range(len(queue)):  # one level
            node = queue.popleft()
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
        view.append(node.val)  # last node popped on this level
    return view
▸Binary Tree Right Side View solution in JavaScript
function rightSideView(root) {
  if (!root) return [];
  const view = [];
  let level = [root];
  while (level.length) {
    view.push(level[level.length - 1].val); // rightmost on this level
    const next = [];
    for (const node of level) {
      if (node.left) next.push(node.left);
      if (node.right) next.push(node.right);
    }
    level = next;
  }
  return view;
}

Common mistakes

  • Only following right children from the root, which misses deeper nodes on left branches.
  • Not separating levels in the BFS, so you cannot tell which node was last on its level.
  • Pushing the right child before the left and then taking the last node, which gives the left side view instead.
  • Forgetting the empty tree case.
  • Using recursion on a very deep, skewed tree in Python and hitting the recursion limit.

What interviewers look for

Start by stating the trap: "it is not just the right spine; a left node is visible when its level has nothing further right." Drawing [7, 3, 9, 2] makes that concrete and shows the interviewer you checked the definition before coding.

Then pick BFS by level and say how you separate levels (snapshot the queue length). Offering the right-first DFS as an alternative, with its O(h) space trade-off, is a good sign that you know both traversals.

Follow-up questions

How would you return the left side view?

Take the first node of each level in the BFS instead of the last, or run the DFS visiting the left child before the right.

What if you wanted the view from the bottom, by horizontal position?

Give each node a column index (root 0, left child col - 1, right child col + 1) and do a BFS. For each column, keep the last node seen; then output columns from smallest to largest.

How would you get both the left and right views in one pass?

In the level-by-level BFS, the first node of each level is the left view and the last is the right view, so record both as you go. On a level with one node they are the same node.

Companies that ask Binary Tree Right Side View

Binary Tree Right Side View is tagged by 93 companies in our interview dataset. The score shows how often it comes up at each company.

  • Aviatrix100%
  • Roche94%
  • Bosch Global93%
  • Ringcentral93%
  • Thermo Fisher Scientific90%
  • Wixcom87.3%
  • Blue Yonder86%
  • Equipmentshare86%
  • Meta85.4%
  • Schneider Electric82%
  • Gemini81%
  • Calix79%
  • Bolt Eu78%
  • Glance77%
  • Latitude Ai77%
  • Elsevier76%
  • Genesys76%
  • Ancestry75%
  • Thryv75%
  • Nebius74%
  • Front73%
  • Stockx73%
  • Vsco72%
  • Intuitive71%

and 69 more.

Practice next

  • All Nodes Distance K in Binary TreeMediumTree to graph + BFS
  • Sum Root to Leaf NumbersMediumDFS with a running prefix value
  • Closest Binary Search Tree ValueEasyWalk one BST search path
  • Number of IslandsMediumFlood fill (DFS/BFS on a grid)

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • AI Interview Copilot
  • Coding Interview Copilot
  • AI Interview Assistant
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.