Link: https://leetcode.com/problems/flood-fill/
Solution:
Intuition
Very simple graph traversal problem. Either DFS or BFS works here…BFS is slightly more efficient so lets use that. The idea is to traverse the component of connected colours and mark them each with the new colour…we can implement this with in-place cycle detection.
The one edge case to consider is if the start node is already the right colour…in this case we just return image…otherwise this causes an infinite cycle.
Implementation
Visual