-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathKSumPaths.cpp
More file actions
35 lines (25 loc) · 751 Bytes
/
KSumPaths.cpp
File metadata and controls
35 lines (25 loc) · 751 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
31
32
33
34
// https://practice.geeksforgeeks.org/problems/k-sum-paths/1
class Solution{
public:
void checkPathSum(Node* root, int k, int &count, vector<int> paths){
if(root==NULL)
return;
paths.push_back(root->data);
checkPathSum(root->left, k, count, paths);
checkPathSum(root->right, k, count, paths);
int sum= 0;
for(int i=paths.size()-1; i>=0; i--){
sum+= paths[i];
if(sum==k)
count++;
}
}
int sumK(Node *root,int k)
{
// code here
vector<int> paths;
int count= 0;
checkPathSum(root, k, count, paths);
return count;
}
};