BFS naturally maps to depth: each full iteration of the while loop processes one complete level. Count the iterations — that count is the depth.
Push the root, then level = 0.
Each while iteration: process every node currently in the queue (one full level), enqueue their children, then level += 1.
When the queue empties, level equals the number of levels = max depth.
The visualization colors each level distinctly and shows the queue refilling with the next level's nodes. The MAX DEPTH counter in the top-right ticks up as each level completes.
STEP—
Press ▶ or step forward to begin.
✓
—
Space Play/Pause ·←→ Step ·R Reset ·F Fullscreen
Executing Code
class Solution:
defmaxDepth(self, root):
q = deque()
if root: q.append(root)
level = 0
while q:
for i inrange(len(q)):
node = q.popleft()
if node.left: q.append(node.left)
if node.right: q.append(node.right)
level += 1
return level
Time: O(n) | Space: O(n) — queue holds at most one full level (up to n/2 nodes)
2. DFS — Recursive O(n) ★
Intuition
The depth of a tree is 1 + max(depth(left), depth(right)). Base case: nullhas depth 0. The recursion unrolls bottom-up — leaves return 1, each internal node adds 1 to the deeper child's result, and the answer bubbles back to the root.
The visualization shows the call stack panel and annotates each node with its computed return value (green bubble) as the recursion unwinds upward.
Purple = currently active frame. Green bubble = return value resolved for that node.
Push (node, depth) pairs onto a stack. Each pop: update res = max(res, depth), then push children with depth + 1. When the stack empties, res holds the answer.
The stack panel shows (val, d=N) pairs; the MAX DEPTH counter tracks res live.
Blue d=N badges appear on visited nodes showing the depth at which they were processed.
STEP—
Press ▶ or step forward to begin.
✓
—
Space Play/Pause ·←→ Step ·F Fullscreen
Executing Code
class Solution:
defmaxDepth(self, root):
stack = [[root, 1]]
res = 0
while stack:
node, depth = stack.pop()
if node:
res = max(res, depth)
stack.append([node.left, depth + 1])
stack.append([node.right, depth + 1])
return res
Time: O(n) | Space: O(n)
Common Pitfalls
Confusing depth with height: depth counts from the root down (root = depth 1). Height counts from leaves up. For this problem both are equivalent (max depth = height), but be consistent — returning 0 for a single-node tree vs. 1 depends on convention. The problem defines single-node = depth 1.
Not handling empty tree: always return 0 for a null root. Accessing root.left without a null check crashes immediately.
BFS: not snapshotting level size: in BFS the inner loop must iterate exactly len(q) times at the start of the outer loop. If you call len(q) inside the loop it changes as you enqueue children, causing wrong level boundaries.
Iterative DFS: forgetting to track depth per node: unlike BFS where depth is implicit in the level count, DFS needs to carry depth explicitly — either as a pair in the stack or via a separate map.
Discussion
…