-
Notifications
You must be signed in to change notification settings - Fork 3
/
797.cpp
45 lines (44 loc) · 1.33 KB
/
797.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
class Solution {
public:
vector<vector<int>> res;
void backtracking(vector<vector<int>>& graph, int node, int target, vector<int>& path) {
if (node == target) {
res.push_back(path);
return;
}
for (auto& neighbor : graph[node]) {
path.push_back(neighbor);
backtracking(graph, neighbor, target, path);
path.pop_back();
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<int> path = {0};
int n = graph.size();
backtracking(graph, 0, n - 1, path);
return res;
}
};
class Solution {
public:
vector<vector<int>> paths;
void backtracking(int parent, int node, int target, vector<int>& path, vector<vector<int>>& graph) {
if (node == target) {
paths.push_back(path);
return;
}
for (auto& neighbor : graph[node]) {
if (neighbor == parent) continue;
path.push_back(neighbor);
backtracking(node, neighbor, target, path, graph);
path.pop_back();
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> path;
path.push_back(0);
backtracking(-1, 0, n - 1, path, graph);
return paths;
}
};