← DSA Atlas
Dedicated problem page · #104

Maximum Depth of Binary Tree

EasyTrees and Binary Search TreesPost-order height aggregationDFS recursion on a binary tree
Solve on LeetCode ↗
104
EasyTrees and Binary Search TreesDFS recursion on a binary treePost-order height aggregation

Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth: the number of nodes along the longest path from the root down to the farthest leaf node.

Open official problem prompt ↗
In plain English

Measure how many levels the tree has — the length in nodes of the longest chain from the root to any leaf.

Picture it like this

Like measuring the height of a family tree by asking each person 'how many generations are below you?', then taking 1 plus the deepest branch.

Example
Input
root = [3, 9, 20, null, null, 15, 7]
Output
3
Why
The longest root-to-leaf path is 3 -> 20 -> 15 (or 3 -> 20 -> 7), which visits 3 nodes.
Constraints
The number of nodes is in the range [0, 10^4]-100 <= Node.val <= 100
Pattern lesson

See the pattern, then code

Post-order height aggregation
Recognition clue

You are asked for a single number that depends on the whole subtree beneath each node — a classic signal for a post-order DFS that combines children's results.

Trees and Binary Search Trees

Hierarchies, subtree aggregation, path properties, or ordered tree queries.. A tree's depth is 1 (for the current node) plus the larger of its two subtree depths; an empty tree contributes depth 0.

New words, made simpleKnow these before the algorithm
Depth / Height
The number of nodes on the longest path from a node down to a leaf.
Post-order
A traversal that fully processes both children before combining their results at the parent.
Leaf
A node with no children.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
BFS level counting

Correct, but uses a queue that can hold a whole level (up to n/2 nodes) and is more code than recursion.

Do a breadth-first traversal and count how many levels you pop.

Time O(n)Space O(n)
The rule we keep true

Invariant

maxDepth(node) always equals the true number of nodes on the longest path from node to a leaf beneath it.

Why this is correct

Reasoning

By induction: null subtrees have depth 0, and if both children report correct depths, then 1 + max(left, right) is exactly the longest path through the current node.

The algorithm in three movesSay these aloud before coding
1If the node is null, return 0

depth(15)=1, depth(7)=1

2Recurse on the left child to get its depth

depth(20)=1+max(1,1)=2

3Recurse on the right child to get its depth

depth(3)=1+max(1,2)=3

4Return 1 + max(left, right)

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
30
91
202
153
74
1 · Readnodes 9, 15, 7
2 · AskDepth of a leaf?
3 · Update stateeach recurses into two null children
4 · Resulteach returns 1 + max(0,0) = 1
Key takeaway

The highlighted path 3 -> 20 -> 15 is the longest, giving depth 3.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 11-12Base case

    An empty subtree has depth 0, which stops the recursion and anchors the counting.

  2. 2
    Lines 13Combine children

    Take the deeper of the two subtrees and add 1 for the current node.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • Empty tree (root is null) returns 0
  • Single node returns 1
  • A completely one-sided (skewed) tree returns n
!

Common beginner mistakes

  • Returning max depth as edge count (off by one) instead of node count
  • Forgetting the null base case, causing an attribute error on None
  • Adding depths of both children instead of taking the max — that would compute a diameter-like value, not depth
Check your understanding

Why do we take max of the children rather than their sum?