Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at Plaid

Return the maximum depth of a binary tree. Plaid asks this as a baseline for harder tree-depth problems on category hierarchies of varying depths from different financial institutions.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid intro problem.
  • LeetCode Discuss (2026)Plaid 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

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

Level-order traversal; count levels.

Time
O(n)
Space
O(w) where w is max width
function maxDepth(root) {
  if (!root) return 0;
  let depth = 0;
  let level = [root];
  while (level.length) {
    depth++;
    const next = [];
    for (const n of level) {
      if (n.left) next.push(n.left);
      if (n.right) next.push(n.right);
    }
    level = next;
  }
  return depth;
}

Tradeoff: Works but allocates per level. For deep skewed trees, recursion is cleaner.

2. Recursive DFS

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

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

Tradeoff: One-liner core. Stack-depth is O(h) which is fine for the LC bound. For 10M-node category trees you'd switch to iterative.

Plaid-specific tips

Plaid uses this as a tree-recursion smoke test. Bonus signal: ask whether the tree is balanced — that bounds recursion depth and informs whether you can use the simple recursive form. For their bank-category trees (worst case ~20 levels), recursive is always safe.

Common mistakes

  • Returning 0 for a leaf — leaves are depth 1.
  • Confusing depth (nodes) with height (edges) — read the problem carefully.
  • Off-by-one when using BFS — incrementing depth at the wrong time.

Follow-up questions

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

  • Minimum depth (LC 111) — first leaf, not deepest.
  • Diameter of a binary tree (LC 543).
  • Is the tree balanced (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

What about an empty tree?

Depth 0 by convention. Both approaches handle this.

Why not always recurse?

Recursion blows the stack for deep skewed trees (~10^4 in V8). LC's bound is 10^4 nodes which can form a depth-10^4 path — tight. BFS is the safer industrial default.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →