Back to problems Solve on LeetCode → ◆ Tree Patterns See #111 Min Depth →

Maximum Depth of Binary Tree

BFS — Level-by-Level Depth Counting

LeetCode 104 • Easy • Trees

Input: root = [3, 9, 20, null, null, 15, 7]  →  Output: 3
The maximum depth is the number of nodes along the longest path from root to the farthest leaf.

Time O(n) visit each node once
Space O(n) queue holds widest level
Visited: 0/5 Queue: 0 Depth: 0
Processing
In Queue
Processed
Edge highlight
Queue
empty
Depth
0
Ready
Press Play to watch BFS count depth level-by-level, or Step to advance one operation at a time.
Each time we fully process a level, depth += 1.