← Stack

Pattern Recognition Drill

#42 — DFS iterative

Easy Graphs

The Problem

Traverse a graph depth-first using an explicit stack.

What approach would you use?

Think about it before scrolling down.

Key Signals

Stack

Push start, loop: pop, visit if unvisited, push neighbors. O(V+E).

Common Trap

Don't forget the visited set — without it, cycles cause infinite loops.

← #41 #43 →