List the Nodes Visible From the Right Side of a Binary Tree

Last updated: November 29, 2026

Quick Overview

Given the root of a binary tree, return the values of the nodes you would see standing to the right of it, from top to bottom, meaning the last node on each level.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the root of a binary tree, return the values of the nodes you would see standing to the right of it, from top to bottom, meaning the last node on each level.

A common Google phone screen opener that checks level-order traversal. Interviewers often follow up by asking for the left view or a DFS version, so they are probing whether you understand why BFS levels map directly onto what is visible.

What the Interviewer Expects
  • Use BFS level by level, tracking level size
  • Take the last node of each level
  • Offer a DFS alternative that visits right child first
  • Handle an empty tree
  • Explain that a left child can be visible when the right subtree is shorter
Key Topics to Cover
binary tree
BFS
level-order traversal
DFS
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the left view instead.
  • Do it with DFS and O(h) extra space.
  • Return the bottom view, using horizontal distance.
  • What changes for an N-ary tree?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: root of a binary tree (may be None).
  • Output: list with one value per depth, the rightmost node at that depth.
  • The rightmost node at a depth may sit in the left subtree when the right su...
High-Level Design

Level-order BFS processes the tree one depth at a time. If I snapshot len(queue) at the start of a level, the nodes popped in that loop are exactly that level, left to right, so the last one popped ...


Submit Your Answer
Markdown supported