LeetCode 797. All Paths From Source to Target

개발공부를해보자·2025년 7월 7일

LeetCode

목록 보기
95/95

Graph Theory 문제 7번
https://leetcode.com/problems/all-paths-from-source-to-target/

나의 풀이(3ms, 93.26%)

DFS, Recursive

class Solution:
    def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
        result = []
        def dfs(node, path):
            if node == len(graph) - 1:
                result.append(path[:])
                return
            for nei in graph[node]:
                path.append(nei)
                dfs(nei, path)
                path.pop()
        
        dfs(0, [0])
        
        return result
profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글