-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113_Path_Sum_II.cc
44 lines (40 loc) · 1.18 KB
/
113_Path_Sum_II.cc
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
/**
* 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:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(root==NULL) return res;
vector<int> tmp;
tmp.push_back(root->val);
getvec(root,tmp,root->val,sum);
return res;
}
private:
vector<vector<int>> res;
void getvec(TreeNode* root,vector<int> tmp,int result,int sum){
if(result!=sum&&root->left==NULL&&root->right==NULL) return;
else if(result==sum&&root->left==NULL&&root->right==NULL) {
res.push_back(tmp);
return;
}
else{
if(root->left) {
vector<int> cptmp1=tmp;
cptmp1.push_back(root->left->val);
getvec(root->left,cptmp1,result+root->left->val,sum);
}
if(root->right) {
vector<int> cptmp2=tmp;
cptmp2.push_back(root->right->val);
getvec(root->right,cptmp2,result+root->right->val,sum);
}
}
}
};