Skip to main content

5. Maximum Depth of Binary Tree

easyAsked at Notion

Return the maximum depth of a binary tree. Notion uses this as a recursion warm-up — page nesting in their workspace is conceptually the same tree.

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

Source citations

Public interview reports confirming this problem appears in Notion loops.

  • Glassdoor (2026-Q1)Notion uses this as the second-easiest tree warmup after node count.
  • Reddit r/cscareerquestions (2025)Reported in Notion frontend phone 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

Example 3

Input
root = []
Output
0

Approaches

1. BFS level counter

Level-order traversal, counting levels.

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

Tradeoff: Correct but verbose. Uses O(w) extra memory.

2. DFS recursion (optimal-style)

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

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

Tradeoff: Three lines, ideal for the warm-up. Stack depth is O(h); for skewed trees that's O(n).

Notion-specific tips

Notion expects this in under 2 minutes. They use it to gauge how comfortable you are recursing on a tree — the real interview is usually a follow-up like 'now find the deepest leaf' or 'now compute this for our page tree where each node has N children, not 2.'

Common mistakes

  • Returning Math.max(...) instead of 1 + Math.max(...) — off-by-one on depth.
  • Forgetting the null base case — recursion never terminates.
  • Confusing 'depth' (root to leaf path nodes) with 'height' (edges) — they differ by 1.

Follow-up questions

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

  • Maximum Depth of N-ary Tree (LC 559) — Notion's actual page tree.
  • Find the deepest leaf and return its value.
  • Compute depth iteratively without recursion.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Recursive or iterative — which does Notion prefer?

They prefer the cleanest correct solution. Recursive is cleaner here; if the tree could be deeper than the JS stack (~10k), switch to iterative BFS.

What does 'depth' mean exactly?

Number of nodes from root to the deepest leaf. A single-node tree has depth 1. An empty tree has depth 0.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →