One Pager Cheat Sheet

  • Using the flood-fill algorithm, we need to think through a smaller input first to traverse an element's neighbors in the given matrix.
  • We are looking to traverse row 0, column 0 to the right, down and bottom right (*).
  • We don't need to traverse the graph with either a Breadth-first search or Depth-first search approach as long as there are matching nodes for the initial one we started with.
  • We can replace the queue we usually use with DFS using either a recursive approach with O(|V|) time and space complexity, or an iterative approach using a stack for the same complexity.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT

That's all we've got! Let's move on to the next tutorial.

If you had any problems with this tutorial, check out the main forum thread here.