Skip to main content

13. Maximum Depth of Binary Tree

easyAsked at Workday

Given the root of a binary tree, return its maximum depth. Workday uses this as the tree-recursion warmup before harder org-chart depth questions like 'find the deepest reporting chain'.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE1 screen — classic tree warmup.

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. Count nodes via traversal

Walk the entire tree and track current depth per node.

Time
O(n)
Space
O(n)
// DFS with explicit depth counter — works but more code than needed
let best = 0;
function go(n, d) { if (!n) return; best = Math.max(best, d); go(n.left, d+1); go(n.right, d+1); }
go(root, 1);
return best;

Tradeoff: Works but doesn't show off the elegance of structural recursion.

2. Structural recursion

Depth of empty tree is 0; otherwise 1 + max(left depth, right depth).

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

Tradeoff: Three lines. The base case + recursive case in canonical form.

Workday-specific tips

Workday loves this as the litmus test for structural recursion. The one-liner is what they want. Bonus: mention that for highly skewed trees (a long reporting chain), iterative BFS with level counting avoids stack overflow.

Common mistakes

  • Returning 0 instead of 1 for a single-node tree — off by one.
  • Confusing depth (nodes) with height (edges) — the prompt is explicit but candidates miss it.
  • Using BFS without level tracking — counts nodes, not depth.

Follow-up questions

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

  • Minimum Depth of Binary Tree (LC 111) — subtle edge case.
  • Diameter of Binary Tree (LC 543).
  • Balanced Binary Tree (LC 110).

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?

The problem says depth = nodes from root to deepest leaf. Empty tree = 0, single node = 1. Edge-counting would shift by one.

Iterative version?

BFS level-by-level, increment a counter per level. Safer for deep trees but more code.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →