릿코드200 - 섬의 수
class Solution: def numIslands(self, grid: List[List[str]]) -> int: ans = 0 m = len(grid) n = len(grid[0]) for i in range(m) : for j in range(n) : if grid[i][j] == "1" : ans += 1 self.check(grid, i, j) return ans def check(self, grid, i, j) : dir = [ [1, 0], [0, 1], [-1, 0], [0, -1] ] stack = [[i,j]] while stack : i, j = stack.pop() grid[i][j] = 0 for direction in dir : X, Y = direction nextX, n..
더보기
릿코드 104 - 트리의 가장 깊은 층
# 재귀 버전 class Solution(object): def pre(self, node, count, depth): count += 1 if node.right: self.pre(node.right, count, depth) if node.left: self.pre(node.left, count, depth) if count > depth[0]: depth[0] = count def maxDepth(self, root): depth = [0] if root: self.pre(root, 0, depth) return depth[0] # iter 버전 class Solution(object): def maxDepth(self, root): stack = [(1, root)] maxDepth = 0 whi..
더보기