-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113. Path Sum II.cpp
42 lines (33 loc) · 987 Bytes
/
113. Path Sum II.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void pathsumrec(TreeNode* root, int sum, vector<vector<int> > &result, vector<int> &cur) {
if (root == NULL)
return;
if (root->val == sum && !root->right && !root->left) {
cur.push_back(root->val);
result.push_back(cur);
cur.pop_back();
return;
}
cur.push_back(root->val);
pathsumrec(root->left, sum - root->val, result, cur);
pathsumrec(root->right, sum - root->val, result, cur);
cout << cur.back() << endl;
cur.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector< int> > result;
vector<int> cur;
pathsumrec(root, sum, result,cur);
return result;
}
};