#104

Maximum Depth of Binary Tree

Easy Recursive height

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

?The problem

Return the number of nodes on the longest path from the root down to a leaf.

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

◎Understanding

Really asking: the number of levels.

Why this works: depth = 1 + deeper of the two subtrees; an empty tree is 0.

Example: 3 (9, 20 (15, 7))

depth(9) = 1, depth(20) = 2depth(3) = 1 + 2 = 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Returning 1 for a null node, which adds one level to everything.

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