생각해보니 번호는 별로 안 중요한거 같음
https://leetcode.com/problems/all-paths-from-source-to-target/
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
queue = deque([])
ans = []
queue.append([0])
while queue:
path = queue.popleft()
cur = path[-1]
for next in graph[cur]:
if next == len(graph)-1 :
ans.append(path + [next])
else :
queue.append(path + [next])
return ans
n이 크지 않고 DAG기 때문에 visit 배열 없이 그냥 BFS 돌리면 끝.
경로는 리스트로 관리. 파이썬 최고!
댓글 0