40 / 75
M

Binary Tree Right Side View

#40
treebfsdfs

Use BFS to traverse level by level, collecting the rightmost node at each level.

Example:

Input:root=[1,2,3,null,5,null,4]
Output:[1,3,4] (rightmost at each level)

Common Mistakes:

  • Only traversing right subtree (misses cases where left is visible)
  • Not handling levels properly (missing rightmost at each level)
  • Confusing with pre-order traversal
  • Not considering left children when right is null

Notes:

BFS approach shown. Can also solve with DFS (right-first traversal, track max depth). Time O(n), Space O(n).

40/75
Binary Tree Right Side View