class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
# time O(N) space O(1)
# main idea: step1: check if start point has the same color the recursion will never end; step2; dfs and set conditions to check 4 directions to avoid out of boundry # step3 return image
width = len(image[0])
height = len(image)
color = image[sr][sc]
def _dfs(sr,sc):
if 0 <= sr < height and 0 <= sc < width and image[sr][sc] == color :
image[sr][sc] = newColor
_dfs(sr+1,sc)
_dfs(sr-1,sc)
_dfs(sr,sc+1)
_dfs(sr,sc-1)
if color != newColor:
_dfs(sr,sc)
return image