C# DFS solution


  • 0
    K
    public class Solution {    
        int r = 0;
        int c = 0;    
        public void DFS(int[,] image, int row, int col, int newColor, int startColor){
            if ( row < 0 || row >= r || col < 0 || col >= c || image[row, col] == newColor || image[row,col] != startColor)
                return;
            image[row, col] = newColor;
            DFS(image, row + 1, col, newColor, startColor);
            DFS(image, row - 1, col, newColor, startColor);
            DFS(image, row, col - 1, newColor, startColor);
            DFS(image, row, col + 1, newColor, startColor);        
        }
        
        public int[,] FloodFill(int[,] image, int sr, int sc, int newColor) {
            r = image.GetLength(0);
            c = image.GetLength(1);
            int startColor = image[sr,sc];
            DFS(image, sr, sc, newColor, startColor);
            return image;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.