-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath Sum II
More file actions
30 lines (28 loc) · 740 Bytes
/
Path Sum II
File metadata and controls
30 lines (28 loc) · 740 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
27
28
29
30
# Competitive-programming-DSA
class Solution {
// leetcode problem 113 pathsum II
public:
vector<vector<int>>res;
void solve(TreeNode *root,int targetSum,int sum,vector<int>vec){
if(!root)return;
sum+=root->val;
if(!root->left && !root->right){
if(sum==targetSum){
vec.push_back(root->val);
res.push_back(vec);
vec.pop_back();
}
return;
}
vec.push_back(root->val);
solve(root->left,targetSum,sum,vec);
solve(root->right,targetSum,sum,vec);
vec.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
int sum=0;
vector<int>vec;
solve(root,targetSum,sum,vec);
return res;
}
};