leetcode

Flood Fill a Matrix (Non Leetcode)

    public void floodfill(int[][] matrix, int x, int y, int newColor) {
        floodfill(matrix, x, y, matrix[x][y], newColor);
    }

    public void floodfill(int[][] matrix, int x, int y, int previous, int current) {
        if (x<0||x>matrix.length-1||y<0||y>matrix[0].length-1) {
            return;
        }

        if (matrix[x][y]!=previous) {
            return;
        }

        matrix[x][y] = current;
        floodfill(matrix, x+1, y, previous, current);
        floodfill(matrix, x-1, y, previous, current);
        floodfill(matrix, x, y+1, previous, current);
        floodfill(matrix, x, y-1, previous, current);
    }