Given a binary tree, explain how you would find its maximum depth (the number of nodes along the longest path from the root to the farthest leaf node).
For example:
3
/ \
9 20
/ \
15 7The maximum depth is 3 (path: 3 -> 20 -> 15 or 3 -> 20 -> 7).
How would you approach this recursively? Could you also solve it iteratively?
Explain your approach and analyze the time and space complexity.
How to approach it
- Hint 1
The depth of a tree is 1 plus the depth of its deepest subtree.
- Hint 2
Recursively, the max depth is 1 + max(maxDepth(left), maxDepth(right)). Base case: a null node has depth 0.
- Hint 3
For an iterative approach, consider BFS with level tracking: the number of levels processed equals the max depth.
Ready to answer it out loud?
Record your answer in 1:00 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.