Skip to main content

12. Maximum Depth of Binary Tree

easyAsked at Adobe

Given the root of a binary tree, return its maximum depth. Adobe uses this to validate recursive thinking that directly mirrors how document tree structures and layer hierarchies are traversed in creative applications.

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

Source citations

Public interview reports confirming this problem appears in Adobe loops.

  • Glassdoor (2025-10)Adobe SDE-II onsite reports cite tree recursion as a recurring warm-up theme.
  • LeetCode Discuss (2026-01)Multiple Adobe candidates report tree depth as a phone-screen opener before harder tree problems.

Problem

Given the root of a binary tree, return its maximum depth. The 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

Explanation: The longest path is 3 -> 20 -> 15 or 3 -> 20 -> 7, both of length 3.

Example 2

Input
root = [1,null,2]
Output
2

Approaches

1. Brute force BFS level-count

Use a queue, process level by level, count the number of levels.

Time
O(n)
Space
O(n)
function maxDepth(root) {
  if (!root) return 0;
  let depth = 0;
  const queue = [root];
  while (queue.length) {
    const size = queue.length;
    for (let i = 0; i < size; i++) {
      const node = queue.shift();
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
    depth++;
  }
  return depth;
}

Tradeoff: BFS is correct but uses O(n) queue space and is wordier; Adobe prefers candidates to reach DFS recursion first.

2. DFS recursive

At each node, recurse left and right, return 1 + max(leftDepth, rightDepth). Base case: null returns 0. This mirrors the natural recursive decomposition of layer trees in Adobe applications.

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

Tradeoff: Elegant one-liner that Adobe interviewers appreciate; stack space is O(h), which for balanced trees is O(log n).

Adobe-specific tips

Adobe interviews frequently probe the relationship between tree depth and real-world document/layer hierarchies. After the solution, be ready to discuss iterative DFS using an explicit stack — Adobe tools teams care about non-recursive implementations for very deep trees to avoid stack overflows in production rendering engines.

Common mistakes

  • Forgetting the base case for null — returning 1 instead of 0 when root is null inflates the answer by 1.
  • Off-by-one: counting edges instead of nodes.
  • Using BFS but miscounting levels by initializing depth = 1 before the loop.

Follow-up questions

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

  • What is the minimum depth of a binary tree (LC 111)?
  • How would you compute depth iteratively without recursion?
  • How does this change for an n-ary tree (LC 559)?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is DFS or BFS preferred here?

Both are O(n) time. DFS is preferred for its conciseness. BFS is useful when you also need to track the depth at each level for other purposes.

What if the tree is heavily skewed?

A skewed tree degrades to O(n) stack depth for recursion. For production code, an iterative DFS with an explicit stack avoids call-stack overflow.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →