Flood Fill
LeetCode 733 | Difficulty: Easyβ
EasyProblem Descriptionβ
You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill:
- Begin with the starting pixel and change its color to `color`.
- Perform the same process for each pixel that is **directly adjacent** (pixels that share a side with the original pixel, either horizontally or vertically) and shares the **same color** as the starting pixel.
- Keep **repeating** this process by checking neighboring pixels of the *updated* pixels and modifying their color if it matches the original color of the starting pixel.
- The process **stops** when there are **no more** adjacent pixels of the original color to update.
Return the modified image after performing the flood fill.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:

From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel.
Example 2:
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation:
The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.
Constraints:
- `m == image.length`
- `n == image[i].length`
- `1 <= m, n <= 50`
- `0 <= image[i][j], color < 2^16`
- `0 <= sr < m`
- `0 <= sc < n`
Topics: Array, Depth-First Search, Breadth-First Search, Matrix
Approachβ
BFS (Graph/Matrix)β
Use a queue to explore nodes level by level. Start from source node(s), visit all neighbors before moving to the next level. BFS naturally finds shortest paths in unweighted graphs.
Shortest path in unweighted graph, level-order processing, spreading/flood fill.
Matrixβ
Treat the matrix as a 2D grid. Common techniques: directional arrays (dx, dy) for movement, BFS/DFS for connected regions, in-place marking for visited cells, boundary traversal for spiral patterns.
Grid traversal, island problems, path finding, rotating/transforming matrices.
Solutionsβ
Solution 1: C# (Best: 256 ms)β
| Metric | Value |
|---|---|
| Runtime | 256 ms |
| Memory | 32 MB |
| Date | 2019-12-31 |
public class Solution {
public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) {
int m = image.GetLength(0);
int n = image[0].GetLength(0);
bool[,] visited = new bool[m,n];
int color = image[sr][sc];
image[sr][sc] = newColor;
Queue<Point> q = new Queue<Point>();
List<int[]> dirs = new List<int[]>()
{
new int[] {1, 0},
new int[] {-1, 0},
new int[] {0, 1},
new int[] {0, -1}
};
q.Enqueue(new Point(sr,sc));
while(q.Count!=0)
{
var top = q.Dequeue();
foreach (var dir in dirs)
{
int xx = top.x + dir[0];
int yy = top.y + dir[1];
if(xx <0 || xx >= m || yy <0 || yy >=n || visited[xx,yy] || image[xx][yy] != color)
{
continue;
}
image[xx][yy] = newColor;
visited[xx,yy] = true;
q.Enqueue(new Point(xx,yy));
}
}
return image;
}
}
public class Point
{
public int x { get; set; }
public int y { get; set; }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
π 1 more C# submission(s)
Submission (2019-12-31) β 260 ms, 31.8 MBβ
public class Solution {
public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) {
int m = image.GetLength(0);
int n = image[0].GetLength(0);
bool[,] visited = new bool[m,n];
int color = image[sr][sc];
image[sr][sc] = newColor;
Queue<Point> q = new Queue<Point>();
List<int[]> dirs = new List<int[]>()
{
new int[] {1, 0},
new int[] {-1, 0},
new int[] {0, 1},
new int[] {0, -1}
};
q.Enqueue(new Point(sr,sc));
while(q.Count!=0)
{
var top = q.Dequeue();
foreach (var dir in dirs)
{
int xx = top.x + dir[0];
int yy = top.y + dir[1];
if(xx <0 || xx >= m || yy <0 || yy >=n || visited[xx,yy] || image[xx][yy] != color)
{
continue;
}
image[xx][yy] = newColor;
visited[xx,yy] = true;
q.Enqueue(new Point(xx,yy));
}
}
return image;
}
}
public class Point
{
public int x { get; set; }
public int y { get; set; }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Graph BFS/DFS | $O(V + E)$ | $O(V)$ |
Interview Tipsβ
- Start by clarifying edge cases: empty input, single element, all duplicates.
- LeetCode provides 1 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.