-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113-Path-Sum-II.cpp
More file actions
26 lines (25 loc) · 864 Bytes
/
113-Path-Sum-II.cpp
File metadata and controls
26 lines (25 loc) · 864 Bytes
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
class Solution {
public:
// same as preOrder.....some additional condn are added
void helper(TreeNode* root, vector<int> v ,vector<vector<int>> &ans, int sum){
if(root==NULL) return;
if(root->left==NULL && root->right==NULL){
if(root->val == sum){
v.push_back(root->val);
ans.push_back(v);
}
return;
}
v.push_back(root->val);
helper(root->left , v , ans, sum-(root->val));
helper(root->right , v , ans, sum-(root->val));
// here targetSum is reduced and when it reaches
// the leaf sum will be(rest) value of leaf node
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> ans;
vector<int> v;
helper(root, v, ans, targetSum);
return ans;
}
};