Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at Snowflake

Find the depth of a binary tree. Snowflake uses this as a recursion warm-up and to set up a follow-up on B-tree fanout — the same calculation determines how many index levels a query traverses.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)Snowflake storage team uses this to lead into B-tree height discussion.
  • LeetCode Discuss (2025-10)Recurring at Snowflake new-grad screens.

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 queue = [root];
  let depth = 0;
  while (queue.length) {
    const next = [];
    for (const n of queue) {
      if (n.left) next.push(n.left);
      if (n.right) next.push(n.right);
    }
    queue = next;
    depth++;
  }
  return depth;
}

Tradeoff: Works, but uses O(w) space — fine on balanced trees, can be O(n/2) on a complete tree.

2. Recursive DFS (optimal for typical trees)

depth(node) = 0 if null else 1 + max(depth(left), depth(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: Cleanest and uses O(h) recursion stack. Note: on extremely skewed trees, an iterative DFS with explicit stack is safer.

Snowflake-specific tips

Snowflake interviewers grade this on cleanliness and on whether you can sketch both BFS and DFS. Bonus signal: discuss B-tree height — given N rows and fanout F, height is log_F(N), which is why Snowflake's storage layer aims for high fanout to keep index traversals shallow.

Common mistakes

  • Returning depth from null as 1 instead of 0 — gives an off-by-one.
  • Summing instead of taking max — that's total node count.
  • Confusing depth of root (1) with height (0 if root is the leaf).

Follow-up questions

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

  • Minimum depth (LC 111) — careful with the null-child case.
  • Diameter of Binary Tree (LC 543).
  • How does B-tree height depend on fanout in Snowflake's storage layer?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Depth or height?

This problem defines depth as number of nodes on the longest root-to-leaf path. Some definitions count edges; LC 104 counts nodes.

Why does fanout matter at Snowflake?

A B-tree with fanout 100 and 1 billion rows has height ceil(log_100(1e9)) ~ 5. With fanout 10, it's 9. Each level is a disk-page fetch. High fanout = fewer page reads per lookup.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →