leetcode-797

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Solution {
public:
void dfs(vector<vector<int>>& g, vector<vector<int>>& res, vector<int> path, int cur) {
path.push_back(cur);
if (cur == g.size() - 1) res.push_back(path);
else for (auto it: g[cur]) dfs(g, res, path, it);
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& g) {
vector<vector<int>> paths;
vector<int> path;
dfs(g, paths, path, 0);
return paths;
}
};