733. Flood Fill#

 1public int[][] floodFill(int[][] image, int sr, int sc, int color) {
 2  int currentColor = image[sr][sc];
 3  dfs(image, sr, sc, currentColor, color);
 4  return image;
 5}
 6
 7private void dfs(
 8  int[][] image,
 9  int m,
10  int n,
11  int currentColor,
12  int newColor
13) {
14  if (m < 0 || m >= image.length || n < 0 || n >= image[0].length) {
15    return;
16  }
17
18  if (image[m][n] == newColor || image[m][n] != currentColor) {
19    return;
20  }
21
22  image[m][n] = newColor;
23
24  dfs(image, m, n + 1, currentColor, newColor);
25  dfs(image, m + 1, n, currentColor, newColor);
26  dfs(image, m, n - 1, currentColor, newColor);
27  dfs(image, m - 1, n, currentColor, newColor);
28}