forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathSum.cpp
More file actions
51 lines (46 loc) · 1.17 KB
/
pathSum.cpp
File metadata and controls
51 lines (46 loc) · 1.17 KB
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
#include <vector>
#include <iostream>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<vector<int> > res;
class Solution {
public:
void doit(TreeNode *root, vector<int>& mm, int sum) {
if (root->left == NULL && root->right == NULL) {
if (root->val == sum) {
mm.push_back(root->val);
res.push_back(mm);
mm.pop_back();
}
return;
}
mm.push_back(root->val);
if (root->left != NULL) {
doit(root->left, mm, sum - root->val);
}
if (root->right != NULL) {
doit(root->right, mm, sum - root->val);
}
mm.pop_back();
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
res.clear();
if (root != NULL) {
vector<int> mm;
doit(root, mm, sum);
}
return res;
}
};
int main() {
return 0;
}