/
BtTargetSumBottomUp.cpp
118 lines (91 loc) · 2.12 KB
/
BtTargetSumBottomUp.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
//20ᵗʰ Dec 2018 (83ʳᵈ) - Number of Paths with a Certain Sum in a Binary Tree (Bottom-up)
#include <iostream>
#include <unordered_map>
using namespace std;
struct Node
{
int val;
struct Node *l;
struct Node *r;
unordered_map<int, int> sumList;
int counter;
};
Node *CreateNode(int val)
{
Node *node = new Node();
node->val = val;
node->l = NULL;
node->r = NULL;
node->counter = 0;
return node;
}
Node *GetData()
{
Node *root = CreateNode(10);
root->l = CreateNode(5);
root->l->l = CreateNode(3);
root->l->l->l = CreateNode(3);
root->l->l->r = CreateNode(-2);
root->l->r = CreateNode(2);
root->l->r->r = CreateNode(1);
root->r = CreateNode(-3);
root->r->r = CreateNode(11);
return root;
}
void AddToHash(Node **t, int val, int counter)
{
auto v = (*t)->sumList.find(val);
if (v == (*t)->sumList.end())
{
(*t)->sumList.insert(pair<int, int>(val, counter));
return;
}
v->second = v->second + counter;
}
void CreateSumList(Node **t, Node **cn, int targetSum)
{
AddToHash(t, (*t)->val + (*cn)->val, 1);
if (targetSum == (*t)->val + (*cn)->val)
(*t)->counter++;
for (auto it = (*cn)->sumList.begin(); it != (*cn)->sumList.end(); ++it)
{
AddToHash(t, (*t)->val + it->first, it->second);
if (targetSum == (*t)->val + it->first)
(*t)->counter = (*t)->counter + it->second;
}
}
void PrintSumList(Node **t)
{
cout << "Node: " << (*t)->val << " Counter: " << (*t)->counter << endl;
for (auto it = (*t)->sumList.begin(); it != (*t)->sumList.end(); ++it)
cout << "(" << it->first << ", " << it->second << ") ";
cout << endl;
}
void Count(Node **t, int targetSum)
{
if (t == NULL)
return;
if ((*t)->l != NULL)
Count(&(*t)->l, targetSum);
if ((*t)->r != NULL)
Count(&(*t)->r, targetSum);
if ((*t)->l != NULL)
{
CreateSumList(t, &((*t)->l), targetSum);
(*t)->counter = (*t)->counter + (*t)->l->counter;
}
if ((*t)->r != NULL)
{
CreateSumList(t, &((*t)->r), targetSum);
(*t)->counter = (*t)->counter + (*t)->r->counter;
}
//PrintSumList(t);
}
int main()
{
Node * root = GetData();
Count(&root, 8);
cout << "Counter: " << root->counter << endl;
getchar();
return 0;
}