← DFS / Recursion

Pattern Recognition Drill

#100 — Flood fill

Easy Advanced Graphs

The Problem

Change all connected cells of the same color starting from (sr, sc).

What approach would you use?

Think about it before scrolling down.

Key Signals

DFS / Recursion

DFS from (sr, sc). Change color, recurse on 4 neighbors matching original color. O(m*n).

Alt: Queue / BFS

BFS from start pixel also works. Same O(m*n).

Common Trap

If new_color == original_color, return immediately — otherwise infinite recursion.

← #99