class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
n = len(graph) - 1
res, path = [], [0]
self.dfs(graph, 0, res, path, n)
return res
def dfs(self, graph, start, res, path, n):
if start == n:
res.append(path.copy())
return
next_idx_list = graph[start]
for next_idx in next_idx_list:
path.append(next_idx)
self.dfs(graph, next_idx, res, path, n)
path.pop()