797. All Paths From Source to Target
797. All Paths From Source to Target
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
dest = len(graph) - 1
res = []
def backtrack(curr, i):
if i == dest:
curr.append(i)
res.append(curr.copy())
curr.pop()
return
curr.append(i)
for node in graph[i]:
backtrack(curr, node)
curr.pop()
backtrack([], 0)
return res