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()
【重点】【回溯】797.所有可能的路径
最新推荐文章于 2025-09-19 19:12:49 发布
1052

被折叠的 条评论
为什么被折叠?



