Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at PayPal

Find the maximum depth of a binary tree. PayPal uses this as a recursion litmus — the same primitive they use to measure the depth of a chain of related transactions when investigating circular fraud rings.

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

Source citations

Public interview reports confirming this problem appears in PayPal loops.

  • LeetCode Discuss (2026-Q1)PayPal new-grad phone screen 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

BFS by level. Increment a counter per level until queue is empty.

Time
O(n)
Space
O(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: Allocates per-level arrays. O(width) memory — can dominate on bushy trees.

2. Recursive DFS (optimal)

Base case null → 0. Else 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: Two lines. PayPal interviewers want you to recognize this is the bedrock of tree-recursion problems.

PayPal-specific tips

PayPal expects you to land the recursive form in under a minute, then ask the follow-up question yourself: 'Should I worry about stack depth on a 10^4-deep skewed tree?' The answer demonstrates production awareness — same concern applies to their transaction-tree traversals.

Common mistakes

  • Returning depth of edges instead of nodes — the problem defines depth as node count.
  • Forgetting the null base — recursing on null throws.
  • Using BFS when DFS is shorter — wastes time in a phone screen.

Follow-up questions

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

  • Minimum Depth (LC 111) — careful, leaf definition matters.
  • Balanced Binary Tree (LC 110).
  • Diameter of Binary Tree (LC 543) — depth-of-subtrees as a helper.

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 1 + max?

The current node contributes 1 to the depth, and below it the deeper of left/right subtree determines the longest path.

Recursion-depth risk?

On a skewed tree of 10^4 nodes, the recursion stack hits 10^4 frames. JS default is usually higher, but it's worth flagging — iterative BFS sidesteps the risk entirely.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →