Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at Vercel

Given a binary tree, return its maximum depth. Vercel asks this to confirm you can do the 'max + 1' recursive pattern — the same trick they use to compute the longest deployment dependency chain.

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel recursion warm-up; expected in 5 minutes.
  • LeetCode Discuss (2026-Q1)Listed in Vercel interview pool.

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

BFS level by level, increment depth per level.

Time
O(n)
Space
O(w) where w is max width
function maxDepth(root) {
  if (!root) return 0;
  let depth = 0;
  let q = [root];
  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 more code than the recursive version. Use this if recursion depth is a concern.

2. Recursive max + 1 (optimal)

Null returns 0. Otherwise 1 + max(depth(left), depth(right)).

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, O(n) time. The base case (null → 0) handles the leaf children naturally.

Vercel-specific tips

Vercel uses this as a baseline; getting it in three lines signals confidence. Bonus signal: pointing out that this is the same recursion shape as diameter (LC 543) and path sum (LC 124), with a different aggregator.

Common mistakes

  • Returning Math.max without the + 1 — off-by-one; you forgot to count the current node.
  • Handling null at the caller via `if (root.left)` checks — works but more code; let the null base case do the work.
  • Counting edges instead of nodes — the problem asks for nodes. Always clarify.

Follow-up questions

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

  • Minimum depth (LC 111) — careful, the recursive shape is subtly different.
  • Balanced binary tree (LC 110) — height check at every node.
  • Diameter of binary tree (LC 543) — longest path between any two leaves.

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 does null return 0?

A null subtree contributes no depth. Returning 0 means the parent node will compute 1 + max(0, sibling) correctly.

Is BFS ever preferred?

When recursion depth might blow the stack — e.g., a skewed tree with 10^5 nodes. JavaScript's stack limit is around 10K frames, so for the LC constraint (10^4) you're fine recursively.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →