Skip to content

Commit 2de81bb

Browse files
authored
Create main.cpp
1 parent cbe35bc commit 2de81bb

File tree

1 file changed

+29
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/27 - K Sum Paths

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
void solve(Node* root, int k, vector<int> path, int &count){
4+
if(root == NULL) return;
5+
6+
path.push_back(root -> data);
7+
8+
solve(root -> left, k, path, count);
9+
solve(root -> right, k, path, count);
10+
11+
int size = path.size();
12+
int sum = 0;
13+
for(int i = size -1; i >= 0; i--){
14+
sum += path[i];
15+
16+
if(sum == k) count++;
17+
}
18+
19+
path.pop_back();
20+
}
21+
int sumK(Node *root, int k) {
22+
vector<int> path;
23+
int count = 0;
24+
25+
solve(root, k, path, count);
26+
27+
return count;
28+
}
29+
};

0 commit comments

Comments
 (0)