-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpath_sum_ii.cpp
More file actions
42 lines (36 loc) · 881 Bytes
/
path_sum_ii.cpp
File metadata and controls
42 lines (36 loc) · 881 Bytes
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
class Solution {
public:
void path_sum(TreeNode *root,
int sum,
vector<int> &path,
vector<vector<int> > &pathes) {
if (NULL == root) {
return;
}
else {
if ((NULL == root->left) && (NULL == root->right)) {
if (root->val == sum) {
path.push_back(sum);
pathes.push_back(path);
path.pop_back();
}
}
else {
path.push_back(root->val);
if (root->left != NULL) {
path_sum(root->left, sum - root->val, path, pathes);
}
if (root->right != NULL) {
path_sum(root->right, sum - root->val, path, pathes);
}
path.pop_back();
}
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > pathes;
vector<int> path;
path_sum(root, sum, path, pathes);
return pathes;
}
};