Flood Fill

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Flood Fill

Approach

Use DFS from the start cell. If a neighbor has the same original color, recolor it and continue. Guard against bounds and same-color checks to stop recursion.

Complexity Analysis

Time
O(m * n)
Space
O(m * n)

Pattern

Grid DFS

Why It Works

All reachable cells with the original color are part of the same connected component, so recursive paint marks exactly that component once.

Updated Feb 2026