forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
113.cpp
71 lines (56 loc) · 1.67 KB
/
113.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
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int> > ans;
if (root == NULL) return ans;
vector<int> path;
pathSumHelper(root, sum, ans, path);
return ans;
}
void pathSumHelper(TreeNode *root, int sum, vector<vector<int> > &ans, vector<int> &path) {
if (root == NULL) {
return;
}
if (sum == root->val && root->left == NULL && root->right == NULL) {
path.push_back(root->val);
ans.push_back(path);
path.pop_back();
return;
}
path.push_back(root->val);
pathSumHelper(root->left, sum - root->val, ans, path);
pathSumHelper(root->right, sum - root->val, ans, path);
path.pop_back();
}
};
int main() {
int sum = 22;
TreeNode *root = new TreeNode(5);
root->left = new TreeNode(4);
root->right = new TreeNode(8);
root->left->left = new TreeNode(11);
root->right->left = new TreeNode(13);
root->right->right = new TreeNode(4);
root->left->left->left = new TreeNode(7);
root->left->left->right = new TreeNode(2);
root->right->right->left = new TreeNode(5);
root->right->right->right = new TreeNode(1);
Solution s;
auto ans = s.pathSum(root, sum);
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans[i].size(); j++) {
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}