[LeetCode] 797. All Paths From Source to Target

김민우·2022년 12월 22일
0

알고리즘

목록 보기
94/189

- Problem

797. All Paths From Source to Target


- 내 풀이

class Solution:
    def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
        answer = []
        N = len(graph)
        q = collections.deque([[0, [0]]])

        while q:
            curr, route = q.popleft()
            if curr == N-1:
                answer.append(route)
            
            else:
                for nei in graph[curr]:
                    q.append([nei, route + [nei]])

        return answer

- 결과

profile
Pay it forward.

0개의 댓글