#199

Binary Tree Right Side View

Medium Rightmost per level

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Imagine standing to the right of the tree. Return the values you can see, from top to bottom.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the last node of every level.

Why this works: DFS that visits right before left; the first node reached at each depth is the one you see.

Example: 1 (2 (-, 5), 3 (-, 4))

depth 0: 1, depth 1: 3, depth 2: 4 → [1, 3, 4]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Only following right children — a deeper left subtree can be visible below a short right side.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h).
Topic: Binary Trees
🎬 Episodes 53–60: video coming soon