-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPathsFromSourceToTarget.cpp
More file actions
33 lines (33 loc) · 1.07 KB
/
AllPathsFromSourceToTarget.cpp
File metadata and controls
33 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
void dfs(unordered_map<int, vector<int>> g, vector<int> curr_path, vector<vector<int>>& path, int currNode, int dest, vector<bool> vis){
if(vis[currNode] == true){
return;
}
if(currNode == dest){
curr_path.push_back(currNode);
path.push_back(curr_path);
return;
}
curr_path.push_back(currNode);
vis[currNode] = true;
for(int i = 0; i<g[currNode].size(); i++){
dfs(g, curr_path, path, g[currNode][i], dest, vis);
}
vis[currNode] = false;
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> res;
int n = graph.size();
unordered_map<int, vector<int> > g;
for(int i = 0; i<graph.size(); i++){
for(int j = 0; j<graph[i].size(); j++){
g[i].push_back(graph[i][j]);
}
}
vector<bool> vis(n, false);
vector<int> curr_path;
dfs(g,curr_path, res, 0, n-1, vis);
return res;
}
};