733. Flood Fill - Detailed Explanation

Problem Statement

Given an m × n image represented by a 2D array image, a starting pixel coordinate (sr, sc), and a newColor, perform a “flood fill” on the image: replace the color of the starting pixel and all pixels connected 4‑directionally (up, down, left, right) that have the same original color with newColor. Return the modified image.

Examples

Input:

image = [[1,1,1],
         [1,1,0],
         [1,0,1]]
sr = 1, sc = 1, newColor = 2

Output:

[[2,2,2],
 [2,2,0],
 [2,0,1]]

Explanation: Starting at pixel (1,1) with original color 1, all connected 1’s are repainted to 2.

Input:

image = [[0,0,0],
         [0,0,0]]
sr = 0, sc = 0, newColor = 0

Output:

[[0,0,0],
 [0,0,0]]

Explanation: newColor is the same as the original color, so no change.

Constraints

  • 1 ≤ m, n ≤ 50
  • 0 ≤ image[i][j], newColor < 2¹⁶
  • 0 ≤ sr < m, 0 ≤ sc < n

Hints

  1. If the starting pixel’s color already equals newColor, you can return immediately.
  2. Use DFS or BFS to explore 4‑directionally connected pixels of the same original color.
  3. Keep track of boundaries and avoid revisiting pixels by checking their color.

Approach 1 – DFS Recursion

Explanation

  1. Record the origColor = image[sr][sc].
  2. If origColor == newColor, return the image unchanged.
  3. Define a recursive function dfs(r, c) that:
    • Checks if (r,c) is out of bounds or image[r][c] != origColor; if so, returns.
    • Sets image[r][c] = newColor.
    • Calls itself on the four neighbors (r+1,c), (r-1,c), (r,c+1), (r,c-1).
  4. Invoke dfs(sr, sc) and return the image.

Step‑by‑step Walkthrough

For the first example:

origColor = 1, newColor = 2
Start at (1,1):
  → paint (1,1) to 2
  → dfs(2,1): color=0 ≠ origColor, stop
  → dfs(0,1): paint to 2, recurse around it
  → dfs(1,2): color=0, stop
  → dfs(1,0): paint to 2, recurse...
Continue until all connected 1’s are repainted

Visual Example

Before:
1 1 1
1 1 0
1 0 1

Paint (1,1):
1 1 1      1 1 1
1 2 0  →   1 2 0
1 0 1      1 0 1

Expand to neighbors:
2 2 2
2 2 0
2 0 1

Python Implementation

Python3
Python3

. . . .

Approach 2 – BFS Iterative

Explanation

Use a queue to perform level‑order traversal:

  1. If origColor == newColor, return immediately.
  2. Initialize queue = deque([(sr, sc)]) and set image[sr][sc] = newColor.
  3. While queue not empty:
    • Pop (r,c).
    • For each neighbor (nr, nc) in the four directions, if in bounds and image[nr][nc] == origColor, repaint it, enqueue it.
  4. Return the image.

Python BFS Code

Python3
Python3

. . . .

Java Implementation (DFS)

Java
Java

. . . .

Complexity Analysis

  • Time: O(m·n) – each pixel is visited at most once.
  • Space:
    • DFS recursion uses O(m·n) stack in worst case.
    • BFS queue uses O(m·n) extra space.

Common Mistakes

  • Not checking origColor == newColor first, causing infinite loops or unnecessary work.
  • Forgetting boundary checks, leading to index errors.
  • Repainting too early or too late (must repaint before enqueueing in BFS or before recursing in DFS).

Edge Cases

  • newColor equals original color → return original image.
  • Single‑pixel image.
  • All pixels already the same color.
  • Non‑rectangular regions (holes) that should not be filled.

Alternative Variations

  • Allow 8‑directional fill (diagonals included).
  • Fill only up to a maximum depth or region size.
  • Perform flood fill on a graph rather than a grid.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
98. Validate Binary Search Tree - Detailed Explanation
Learn to solve Leetcode 98. Validate Binary Search Tree with multiple approaches.
18. 4Sum - Detailed Explanation
Learn to solve Leetcode 18. 4Sum with multiple approaches.
1046. Last Stone Weight - Detailed Explanation
Learn to solve Leetcode 1046. Last Stone Weight with multiple approaches.
3223. Minimum Length of String After Operations - Detailed Explanation
Learn to solve Leetcode 3223. Minimum Length of String After Operations with multiple approaches.
2657. Find the Prefix Common Array of Two Arrays - Detailed Explanation
Learn to solve Leetcode 2657. Find the Prefix Common Array of Two Arrays with multiple approaches.
2017. Grid Game - Detailed Explanation
Learn to solve Leetcode 2017. Grid Game with multiple approaches.
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.