개발 로그/알고리즘
797. All Paths From Source to Target.cpp
k._.
2021. 9. 30. 12:21
Graph -> dfs (depth first search)
class Solution {
public:
int target;
vector<vector<int>> res;
vector<int> tmp;
void dfs(vector<vector<int>>& graph, int currNode = 0) {
tmp.push_back(currNode);
if (currNode == target) res.push_back(tmp);
else {
for (int node: graph[currNode]) {
dfs(graph, node);
}
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
target = graph.size() - 1;
dfs(graph);
return res;
}
};