leetcode 797. 所有可能的路径 medium
题目描述:
解题思路:
dfs, 因为是无环图,所以不需要visited数组
代码:
//
class Solution {
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> ret;
vector<int> path{0};
dfs(ret, path, graph, 0);
return ret;
}
void dfs(vector<vector<int>> &ret, vector<int> &path, vector<vector<int>> &graph, int cur){
if (cur == graph.size() - 1){
ret.push_back(path);
return;
}
for (int i: graph[cur]){
path.push_back(i);
dfs(ret, path, graph, i);
path.pop_back();
}
}
};