Skip to main content

13. Maximum Depth of Binary Tree

easyAsked at Reddit

Compute the depth of a binary tree. Reddit asks this to gauge basic recursion — the same primitive used to measure how deeply a comment tree can nest before they collapse it with 'continue this thread →' links.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen, used as a warmup for comment-tree collapse policy follow-ups.
  • Blind (2025-09)Reported in Reddit comments-team rounds.

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 with level count

Walk the tree level by level, increment depth per level.

Time
O(n)
Space
O(w)
function maxDepth(root) {
  if (!root) return 0;
  const q = [root]; let depth = 0;
  while (q.length) {
    let size = q.length;
    while (size--) {
      const n = q.shift();
      if (n.left) q.push(n.left);
      if (n.right) q.push(n.right);
    }
    depth++;
  }
  return depth;
}

Tradeoff: Works. O(w) extra memory where w is max width. Verbose for what is fundamentally one line of recursion.

2. Recursive DFS (optimal)

Return 0 for null. Otherwise 1 + max of left/right depth.

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

Tradeoff: One-liner. O(h) recursion. For Reddit-scale trees (worst-case depth ~few hundred) this is fine.

Reddit-specific tips

Reddit interviewers expect the recursive one-liner first. Bonus signal: discuss that comment-tree depth is bounded in their product (UI collapses past depth 10), so a hard depth limit can be enforced during traversal to bail early.

Common mistakes

  • Returning the height (edges) instead of depth (nodes) — off by one.
  • Initializing depth to 1 even when root is null.
  • Using Math.max(left, right) + 1 but forgetting to add the +1 outside Math.max.

Follow-up questions

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

  • Minimum depth (LC 111) — tricky because of one-sided subtrees.
  • Balanced binary tree (LC 110) — uses depth as a building block.
  • Diameter of binary tree (LC 543) — depth + global max.

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 depth measured in edges or nodes?

This problem says nodes (so [1] has depth 1). Be careful — some variants ask edges.

Why not iterative DFS?

It works but the recursion stack is the natural model. For Reddit comment depths bounded at ~10, recursion is safe.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →