Tree•Pattern: BFS / DFS•Medium

Binary Tree right side view

Problem Intuition & Objective

Return the values of the rightmost node at each level.

Time ComplexityO(n)
Optimal
Space ComplexityO(n)
Memory Bounds
Optimal Algorithmic Approach

BFS (last of each level) or right-first DFS.

Core Invariants & Edge Cases

  • BFS: take the last element of each level
  • DFS: pass depth, record node value if depth == result.size()
  • Right-first DFS naturally visits the rightmost node first at each depth
Previous ProblemLowest Common Ancestor (LCA) of a binary treeNext Problem Diameter of a binary tree

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.