Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at Asana

Find the maximum depth of a binary tree. Asana asks this to confirm you can write the simplest tree recursion cleanly — a baseline before they ask about deep project-hierarchy traversals.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Asana loops.

  • Glassdoor (2026-Q1)Asana phone-screen tree warmup.

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

Input
root = [3,9,20,null,null,15,7]
Output
3

Example 2

Input
root = [1,null,2]
Output
2

Approaches

1. BFS level count

Walk level by level, increment a depth counter.

Time
O(n)
Space
O(w) where w is max level width
function maxDepth(root) {
  if (!root) return 0;
  let q = [root], d = 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; d++;
  }
  return d;
}

Tradeoff: Works but uses O(w) queue space. For balanced trees w can be n/2.

2. Recursive max of subtree depths

depth(node) = 1 + max(depth(left), depth(right)). Base case: null returns 0.

Time
O(n)
Space
O(h) stack
function maxDepth(root) {
  if (!root) return 0;
  return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}

Tradeoff: Three lines. The base case is the only thing to get right.

Asana-specific tips

At Asana, the bonus signal is articulating the recursive invariant in words: 'the depth of a tree is one more than the deeper of its two subtrees.' If you also note that BFS gives the same answer but uses level-width space, you've covered both viewpoints.

Common mistakes

  • Returning 0 for a single-node tree (should be 1).
  • Confusing depth (root counts) with height (edge count) — they differ by one.
  • Off-by-one on the BFS level counter.

Follow-up questions

An interviewer at Asana may pivot to one of these next:

  • Minimum depth (LC 111) — note the leaf-only twist.
  • Diameter of binary tree (LC 543).
  • Balanced binary tree (LC 110).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is min-depth not just 'replace max with min'?

Because the min must end at a leaf. min(left, right) on a node with one null child would return 0, giving 1 — but that path doesn't end at a leaf.

Stack overflow on deep trees?

Possible at h ~ 10^4 in JS. The iterative BFS or an iterative DFS with explicit stack avoids it.

Practice these live with InterviewChamp.AI

Drill Maximum Depth of Binary Tree and other Asana interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →