Return the Rightmost Category Visible at Each Tree Level

Last updated: November 29, 2026

Quick Overview

Given a binary tree of product categories, return the value of the node you would see at each depth if you looked at the tree from the right side, ordered from root to deepest level.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a binary tree of product categories, return the value of the node you would see at each depth if you looked at the tree from the right side, ordered from root to deepest level.

A level-order traversal question used in Amazon phone screens. The interviewer checks that you can do BFS by level or a DFS that visits right children first, and that you recognize a left-only subtree can still be visible.

What the Interviewer Expects
  • Use BFS and take the last node of each level
  • Or DFS right-first recording the first node seen per depth
  • Explain why left nodes can be visible
  • Give time and space in terms of height and width
  • Handle an empty tree
Key Topics to Cover
Binary Trees
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
  • How would you return the left view and right view together?
  • How would you compute the bottom view?
  • What if the tree is too deep for recursion?
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

Nodes have val, left, right. Root may be None (return an empty list)....

High-Level Design

BFS: process the queue one level at a time; the last node popped in each level is the visible one. Iterative, so no recursion limit.

DFS alternative: recurse right child before left, passing depth; w...


Submit Your Answer
Markdown supported