Skip to content
LC-0797 Medium LeetCode

797. All Paths From Source to Target

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 83% Topics: Backtracking, Depth-First Search, Breadth-First Search, Graph
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(p + r * n), p is the count of all the possible paths in graph,
#                      r is the count of the result.
# Space: O(n)

class Solution(object):
    def allPathsSourceTarget(self, graph):
        """
        :type graph: List[List[int]]
        :rtype: List[List[int]]
        """
        def dfs(graph, curr, path, result):
            if curr == len(graph)-1:
                result.append(path[:])
                return
            for node in graph[curr]:
                path.append(node)
                dfs(graph, node, path, result)
                path.pop()

        result = []
        dfs(graph, 0, [0], result)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT