0797.All Paths From Source to Target
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void dfs(vector<vector<int>>& graph, int current) {
if(current == graph.size() - 1) {
result.emplace_back(path);
return;
}
for(int i = 0; i < graph[current].size(); i++) {
path.emplace_back(graph[current][i]);
dfs(graph, graph[current][i]);
path.pop_back();
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
path.emplace_back(0);
dfs(graph, 0);
return result;
}
};