← DFS / Recursion

Pattern Recognition Drill

#43 — DFS recursive

Easy Graphs

The Problem

Traverse a graph depth-first using recursion.

What approach would you use?

Think about it before scrolling down.

Key Signals

DFS / Recursion

Visit node, mark visited, recurse on unvisited neighbors. O(V+E).

Alt: Stack

Iterative DFS with explicit stack — avoids stack overflow on very deep graphs.

Common Trap

Python's recursion limit (default 1000) can be hit on large graphs. sys.setrecursionlimit or use iterative.

← #42 #44 →