We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cbe35bc commit 2de81bbCopy full SHA for 2de81bb
17 - Binary Tree Data Structure Problems/27 - K Sum Paths/main.cpp
@@ -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