13. Maximum Depth of Binary Tree
easyAsked at CoinbaseReturn the depth of a binary tree. Coinbase uses this to verify recursion fundamentals before moving to harder tree problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Coinbase loops.
- Glassdoor (2026-Q1)— Coinbase warm-up.
Problem
Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Constraints
The number of nodes in the tree is in the range [0, 10^4].-100 <= Node.val <= 100
Examples
Example 1
root = [3,9,20,null,null,15,7]3Example 2
root = [1,null,2]2Approaches
1. BFS level counter
Level-order traversal counting levels.
- Time
- O(n)
- Space
- O(w) for width w
function maxDepth(root) {
if (!root) return 0;
let q = [root], depth = 0;
while (q.length) {
const next = [];
for (const n of q) {
if (n.left) next.push(n.left);
if (n.right) next.push(n.right);
}
q = next; depth++;
}
return depth;
}Tradeoff: Works, but allocates per level. Useful when the prompt forbids recursion.
2. Recursive 1 + max(left, right)
Base case: null → 0. Otherwise 1 + max(maxDepth(left), maxDepth(right)).
- Time
- O(n)
- Space
- O(h)
function maxDepth(root) {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}Tradeoff: Three lines. The cleanest recurrence in DSA.
Coinbase-specific tips
Coinbase grades the one-line recurrence. They'll often follow with 'return the deepest leaf's value' or 'list all leaves at max depth' — recognize these as the same recursion with extra bookkeeping.
Common mistakes
- Returning 0 for null root and then forgetting to start counting at 1 for non-null.
- Tracking depth via global variable — unnecessary indirection.
- Confusing 'depth' (root = 1) with 'height' (root depth = 0).
Follow-up questions
An interviewer at Coinbase may pivot to one of these next:
- Minimum depth (LC 111) — careful with single-child nodes.
- Return the deepest leaf's value.
- Diameter of the tree (LC 543).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Is depth zero-indexed or one-indexed here?
One-indexed: a single-node tree has depth 1, an empty tree has depth 0. Always clarify the convention before coding.
When would BFS beat DFS?
When recursion depth would overflow the native stack (skewed tree with 10^4 nodes). BFS uses a heap-allocated queue with bounded width.
Practice these live with InterviewChamp.AI
Drill Maximum Depth of Binary Tree and other Coinbase interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →