Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at Palantir

Given the root of a binary tree, return its maximum depth. Palantir uses this to gauge whether you treat tree depth as a recursive max problem — the same primitive they use to compute the longest dependency chain in an ETL DAG before deciding scheduler timeouts.

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

Source citations

Public interview reports confirming this problem appears in Palantir loops.

  • Glassdoor (2026-Q1)Palantir FDE phone screen — reported as a 5-minute opener.
  • Reddit r/cscareerquestions (2025-12)Cited as a setup for a DAG longest-path follow-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

Example 3

Input
root = []
Output
0

Approaches

1. Recursive DFS

Depth at each node = 1 + max(depth(left), depth(right)); 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: One-liner. Risk: deep skewed trees may overflow the JS call stack.

2. Iterative BFS level counting

BFS one level at a time; increment depth per level.

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

Tradeoff: Survives deep skewed trees because state lives in the heap. Slight overhead allocating per-level arrays.

Palantir-specific tips

Palantir interviewers expect you to volunteer the iterative version when production safety matters — ontology depth in Foundry is unbounded by design (users define their own hierarchy). Mention that the same DFS shape extends to a DAG longest-path with memoization (Pattern: depth(node) = 1 + max(depth(parent)) for all parents in topo order). That signal of generalizing tree to DAG is what graduates you from junior to senior at Palantir.

Common mistakes

  • Returning Math.max(depth(left), depth(right)) without the +1 — gives 0 for a single-node tree.
  • Counting edges instead of nodes — depth of a single-node tree is 1, not 0.
  • Using BFS but forgetting to capture the level boundary, so depth becomes node count instead.

Follow-up questions

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

  • Minimum depth of binary tree (LC 111) — careful: must reach a leaf.
  • Balanced binary tree check (LC 110) — uses depth recursively.
  • Longest path in a DAG of ETL tasks — topo sort + memoized depth.

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 not just BFS and count nodes?

Counting nodes gives you size, not depth. You need to track when one BFS level ends and the next begins — usually via queue length snapshot at each iteration.

What's the depth of an empty tree?

0. The recursion base case (!root) returns 0, which is what LeetCode expects. Some definitions use -1, but stick with 0 unless told otherwise.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →