LeetCode 797:所有路径从源出发 | DFS

发布时间:2026/5/28 21:39:13

LeetCode 797:所有路径从源出发 | DFS LeetCode 797所有路径从源出发 | DFS引言所有路径从源出发All Paths From Source是 LeetCode 第 797 题难度为 Medium。题目要求找出从源节点到目标节点的所有路径。算法实现def allPathsSourceTarget(graph): target len(graph) - 1 result [] def dfs(node, path): if node target: result.append(path[:]) return for neighbor in graph[node]: path.append(neighbor) dfs(neighbor, path) path.pop() dfs(0, [0]) return result复杂度分析时间复杂度O(V E)空间复杂度O(V)总结DFS 遍历图记录路径。

相关新闻