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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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 ...