-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllPathsBetweenTwoNodes.cpp
59 lines (51 loc) · 1.6 KB
/
AllPathsBetweenTwoNodes.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//https://leetcode.com/contest/weekly-contest-75/problems/all-paths-from-source-to-target/
class Solution {
public:
vector<vector<int>> FindPath(vector<bool> visited, int node, vector<int> path, vector<vector<int>> graph)
{
vector<vector<int>> ret;
if(visited[node] == true)
{
return ret;
}
else
{
visited[node] = true;
path.push_back(node);
if(node==graph.size() - 1)
{
ret.push_back(path);
return ret;
}
else
{
for(int i=0; i<graph[node].size(); i++)
{
vector<vector<int>> allPaths = FindPath(visited, graph[node][i], path, graph);
for(int j=0; j<allPaths.size(); j++)
{
ret.push_back(allPaths[j]);
}
}
return ret;
}
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<bool> visited;
vector<int> path;
visited.resize(graph.size());
visited[0] = true;
path.push_back(0);
vector<vector<int>> allPaths;
for(int i=0; i<graph[0].size(); i++)
{
vector<vector<int>> ret = FindPath(visited, graph[0][i], path, graph);
for(int j=0; j<ret.size(); j++)
{
allPaths.push_back(ret[j]);
}
}
return allPaths;
}
};