forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k_sum_paths.cpp
105 lines (95 loc) · 2.33 KB
/
k_sum_paths.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Given a binary tree, print all the paths whose sum is equal to k
* 1
* / \
* 3 -1
* / \ / \
* 2 1 4 5
* / / \ \
* 1 1 2 6
*
* For k = 5:
* 3 2
* 3 1 1
* 1 3 1
* 4 1
* 1 -1 4 1
* -1 4 2
* 5
* 1 -1 5
*/
#include <iostream>
#include <vector>
struct TreeNode
{
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int d) : data {d}, left {nullptr}, right {nullptr} { }
};
void printPath(const std::vector<int>& path, int index)
{
for (auto it = path.begin() + index; it != path.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
}
void printKPaths(TreeNode * node, std::vector<int>& paths, int k)
{
if (!node)
{
return;
}
// push the current node to paths
//
paths.push_back(node->data);
// now, lets explore the left and right sub-trees
//
printKPaths(node->left, paths, k);
printKPaths(node->right, paths, k);
// check if we have reached sum of tree nodes to k.
// also, do not break, once we have reached k, as we may have negative
// values too.
//
/*
int sum = 0;
for (int j=paths.size()-1; j>=0; j--)
{
sum += (paths[j]);
if (sum == k)
{
printPath(paths, j);
}
}
*/
int sum = 0;
for (auto it = paths.rbegin(); it != paths.rend(); ++it)
{
sum += (*it);
if (sum == k)
{
int indexFromStart = paths.size() - 1 - (it - paths.rbegin());
printPath(paths, indexFromStart);
}
}
paths.pop_back();
}
int main()
{
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(3);
root->left->left = new TreeNode(2);
root->left->right = new TreeNode(1);
root->left->right->left = new TreeNode(1);
root->right = new TreeNode(-1);
root->right->left = new TreeNode(4);
root->right->left->left = new TreeNode(1);
root->right->left->right = new TreeNode(2);
root->right->right = new TreeNode(5);
root->right->right->right = new TreeNode(2);
int k = 5;
std::vector<int> paths;
printKPaths(root, paths, k);
return 0;
}