Easy
Maximum Depth of Binary Tree
👶 The 5-Year-Old Summary
How many generations are in your family tree? Start from grandfather (root), count each generation down to the youngest grandchild!
🎮 Interactive Visualizer
Call Stack:
🧠The Brain Hack
Recursion! Depth = 1 + max(left_depth, right_depth). Go all the way down left, then all the way down right, take the max!
💻 The Clean Solution
def max_depth(root):
# Base case: empty tree has depth 0
if not root:
return 0
# Recursive case: depth is 1 + max of children
left_depth = max_depth(root.left)
right_depth = max_depth(root.right)
return 1 + max(left_depth, right_depth)
# Tree:
# 3
# / \
# 9 20
# / \
# 15 7
#
# Answer: 3
Time: O(n) - visit every node
Space: O(h) - recursion stack, h=height