Maximum Depth of Binary Tree

A node's depth is one more than its deeper subtree.

The Call Stack and Its Returns

max_depth(3): not None, so first find how deep its left subtree goes.
StopBinary treeExport
root
3, active, node3N9, not reached920, not reached2015, not reached157, not reached7
now at3
left9
right20
  • this step
  • on the call stack
  • done, in order
  • not reached
call stack
bottom
  1. max_depth(3)
top
  • CH1 node
  • CH4 call stack

Problem

Given the root of a binary tree, determine how many nodes appear on the longest path from the root down to any leaf. Return 0 when the tree is empty. You may solve this by recursively combining subtree depths or by counting the levels visited in a breadth-first traversal.

Examples

Input: root = [3,9,20,null,null,15,7]

Output: 3

The longest root-to-leaf paths are 3 → 20 → 15 and 3 → 20 → 7, each containing 3 nodes.

Input: root = [1,null,2]

Output: 2

The only root-to-leaf path is 1 → 2, so the tree has depth 2.

The Goal

Find how many levels the tree has: the number of nodes on its longest path from the root down to a leaf. An empty tree has depth 0.

For [3,9,20,null,null,15,7], the longest paths are 3 → 20 → 15 and 3 → 20 → 7, so the depth is 3.

Count nodes, not edges

A single node has depth 1, and an empty tree has depth 0. Count the nodes on the path, not the edges between them.

A leaf is 1 deep.

Key Idea: Ask Each Subtree

A node's depth is one more than the deeper of its two subtrees:

max_depth(node) = 1 + max(max_depth(node.left), max_depth(node.right))

An empty child is 0 deep, which ends the recursion. Each call waits for its left subtree's answer, then its right's, then returns. The answers flow up from the leaves.

A node returns 1 plus the larger of its two depths.

20 returns 1 + max(1, 1) = 2.

The Algorithm

python
def max_depth(node):
    if node is None:
        return 0
    left = max_depth(node.left)
    right = max_depth(node.right)
    return 1 + max(left, right)

Breadth-first search works too: count the levels a level-order traversal drains. Both visit every node once.

ApproachHow it countsExtra space
Recursive DFS1 + the deeper subtreeO(h) call stack
BFS by levelsOne per level drainedO(w) queue, w the widest level

Walkthrough

Run it on [3,9,20,null,null,15,7].

max_depth(3) calls itself on 9 first. Both of 9's children are None and return 0, so 9 returns 1.

Then 3 waits on 20. Its children, 15 and 7, are leaves that return 1 each, so 20 returns 1 + max(1, 1) = 2.

Back at the root, left = 1 and right = 2, and it returns 3.

Checkpoint · predict the next step

Step 9 of 26 on the stage: max_depth(20): not None, so first find how deep its left subtree goes.

node on 20 · call stack = [max_depth(3) · left 1, max_depth(20)] · 9 returned 1

What does step 10 show?
Step through it on the stage

Pitfalls

  • Return 0 for an empty child, not 1, or every depth comes out one too big.
  • Take the larger side. [1,2,null,3,null,4] leans all the way left and is 4 deep.
  • Count nodes, not edges: a single node is 1 deep.
  • A chain of 10,000 nodes recurses 10,000 calls deep, past Python's default limit. Counting levels with a queue avoids that.

What should max_depth(None) return?

Complexity

Each node gets one call, and each call does constant work besides its two recursive calls, so time is O(n).

The call stack holds one call per level of the current path, so space is O(h), where h is the tree's height. That's O(log n) for a balanced tree and O(n) for a chain.

Recursive depth-first search

Time
O(n)
Space
O(h)
Where the time goes
  • Visit each nodeOne call each.dominantO(n)
  • Empty childrenn + 1 of them, each returning 0.dominantO(n)
  • Call stackOne call per level of the path.O(h)
How it grows
input size n →opsO(n²)O(n log n)O(n)O(log n)O(1)
Roughly what that costs
n101001,00010,000
O(n)101001K10K

Linear time, and a stack as deep as the tree.

In short

Recurse: the depth of a node is 1 + max(depth(left), depth(right)), with null contributing 0. A BFS level count works too. Both visit every node once.

TimeO(n)SpaceO(h)

Common mistake

Off-by-one at the base case — a null child is depth 0, a leaf is depth 1.