-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq4.12.cpp
150 lines (115 loc) · 3.7 KB
/
q4.12.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <algorithm> // find
#include <iostream>
#include <iterator> // distance
#include <map>
#include <vector>
using namespace std;
typedef map<int, int> MAP;
typedef map<int, int>::iterator MapIterator;
typedef vector<int> VECTOR;
typedef vector<int>::iterator VecIterator;
class BinTreeNode {
public:
int data;
BinTreeNode *left;
BinTreeNode *right;
int level;
BinTreeNode();
BinTreeNode(int);
void printTree();
};
BinTreeNode::BinTreeNode() : data(0), left(NULL), right(NULL), level(0) {}
BinTreeNode::BinTreeNode(int data)
: data(data), left(NULL), right(NULL), level(0) {}
void BinTreeNode::printTree() {
BinTreeNode *head = this;
if (head->right != NULL)
head->right->printTree();
for (int it = 0; it < this->level; it++)
cout << " ";
cout << head->data << "\n";
if (head->left != NULL)
head->left->printTree();
return;
}
int getArrayIndex(VECTOR *vec, int val) {
VecIterator findIterator;
findIterator = find(vec->begin(), vec->end(), val);
if (findIterator == vec->end())
return -1;
else
return distance(vec->begin(), findIterator);
}
BinTreeNode *buildTree(VECTOR *preorder, VECTOR *inorder, int &preIndex,
int lindex, int rindex, int level) {
int inorderSearchIndex;
if (lindex > rindex)
return NULL;
if ((uint)preIndex >= preorder->size())
return NULL;
// cout << lindex << rindex << preIndex << '\n';
// if(rindex == 9 || preIndex == 9){
// cout << "error here";
// return NULL;
// }
BinTreeNode *node = new BinTreeNode(preorder->at(preIndex));
node->level = level;
inorderSearchIndex = getArrayIndex(inorder, node->data);
preIndex++;
if (lindex == rindex)
return node;
node->left = buildTree(preorder, inorder, preIndex, lindex,
inorderSearchIndex - 1, level + 1);
node->right = buildTree(preorder, inorder, preIndex, inorderSearchIndex + 1,
rindex, level + 1);
return node;
}
bool isKeyExists(MAP &hashmap, int key) {
MapIterator findIterator;
findIterator = hashmap.find(key);
if (findIterator == hashmap.end())
return false;
else
return true;
}
void countPathsWithSum(BinTreeNode *head, int targetSum, int &totalCount,
int cumsum, MAP &hashmap) {
if (head == NULL)
return;
cumsum += head->data;
if (isKeyExists(hashmap, cumsum))
hashmap[cumsum]++;
else
hashmap.insert(make_pair(cumsum, 1));
int diffCumSum = cumsum - targetSum;
if (isKeyExists(hashmap, diffCumSum)) {
totalCount += hashmap[diffCumSum];
if (diffCumSum == cumsum) // this might happen when targer sum is 0
totalCount--;
}
if (head->left != NULL)
countPathsWithSum(head->left, targetSum, totalCount, cumsum, hashmap);
if (head->right != NULL)
countPathsWithSum(head->right, targetSum, totalCount, cumsum, hashmap);
// decrease cumsum count before returning from stack
hashmap[cumsum]--;
if (hashmap[cumsum] == 0)
hashmap.erase(cumsum);
return;
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
/* SOMETHING IS WORNG WITH CREATING A BINARY TREE*/
VECTOR preorder{10, 5, 3, 3, -2, 2, 1, -3, 11};
VECTOR inorder{3, 3, -2, 5, 2, 1, 10, -3, 11};
int preIndex = 0;
BinTreeNode *head =
buildTree(&preorder, &inorder, preIndex, 0, preorder.size() - 1, 0);
if (head != NULL)
head->printTree();
int totalCount = 0;
int targetSum = 8;
MAP hashmap;
countPathsWithSum(head, targetSum, totalCount, 0, hashmap);
cout << "total number of paths with targetSum 8 are: " << totalCount;
return 0;
}