‹ DS&A interview · Socratic
DSA · Trees & BFS · #45

Binary Tree Right Side View

Module 49 · difficulty 3/5·30:00starts on first keystroke

Given the `root` of a binary tree, imagine yourself standing on the **right** side of it. Implement `rightSideView(root)` returning an array of the node values **ordered top to bottom** that you would see — i.e. the last node at each depth level. Each tree node is `{ val, left, right }` with `left`/`right` being `null` when absent.

Examples
  • root = [1,2,3,null,5,null,4] [1,3,4]Levels: [1], [2,3], [5,4]. Rightmost of each: 1, 3, 4.
  • root = [1,null,3] [1,3]Right child only; both nodes are rightmost on their level.
  • root = [] []Empty tree yields an empty view.
Constraints
  • · The number of nodes is in the range [0, 100].
  • · -100 <= Node.val <= 100
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.