-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath Sum II.cpp
34 lines (34 loc) · 915 Bytes
/
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
/**
* 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>> res;
vector<int> path;
void helper(TreeNode* root, int sum){
if(!root->left&&!root->right)
if(sum==0) res.push_back(path);
if(root->left){
path.push_back(root->left->val);
helper(root->left,sum-root->left->val);
path.pop_back();
}
if(root->right){
path.push_back(root->right->val);
helper(root->right,sum-root->right->val);
path.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(!root) return res;
path.push_back(root->val);
helper(root,sum-root->val);
return res;
}
};