-
Notifications
You must be signed in to change notification settings - Fork 0
/
#110.cpp
62 lines (53 loc) · 1.14 KB
/
#110.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
60
61
62
#include<iostream>
#include<vector>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int v) {
val = v;
left = nullptr;
right = nullptr;
}
};
void dfs(Node* curr, vector<int> curr_path, vector<vector<int>> &paths) {
curr_path.push_back(curr->val);
if (curr->left == nullptr && curr->right == nullptr) {
paths.push_back(curr_path);
} else if (curr->left == nullptr) {
dfs(curr->right, curr_path, paths);
} else if (curr->right == nullptr) {
dfs(curr->left, curr_path, paths);
} else {
dfs(curr->left, curr_path, paths);
dfs(curr->right, curr_path, paths);
}
}
vector<vector<int>> getAllPaths(Node* root) {
vector<vector<int>> paths;
vector<int> curr_path;
dfs(root, curr_path, paths);
return paths;
}
int main() {
Node* n1 = new Node(1);
Node* n2 = new Node(2);
Node* n3 = new Node(3);
Node* n4 = new Node(4);
Node* n5 = new Node(5);
n1->left = n2;
n1->right = n3;
n3->right = n5;
n3->left = n4;
vector<vector<int>> paths = getAllPaths(n1);
cout << "[";
for (vector<int> path : paths) {
cout << "[";
for (int i : path) {
cout << i << ",";
}
cout << "], ";
}
cout << "]" << endl;
}