-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.h
More file actions
158 lines (142 loc) · 3.55 KB
/
binarytree.h
File metadata and controls
158 lines (142 loc) · 3.55 KB
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
151
152
153
154
155
156
157
158
#include <stack>
#include <queue>
#include <vector>
template <typename T>
struct btnode {
T val;
btnode *left, *right;
btnode(T v) : val(v), left(NULL), right(NULL) {}
};
/* recursive traversals */
void ltraverse(btnode *node, void (*visit)(btnode*))
{
if (!node) return;
(*visit)(node);
ltraverse(node->left, visit);
ltraverse(node->right, visit);
}
void ctraverse(btnode *node, void (*visit)(btnode*))
{
if (!node) return;
ctraverse(node->left, visit);
(*visit)(node);
ctraverse(node->right, visit);
}
void rtraverse(btnode *node, void (*visit)(btnode*))
{
if (!node) return;
rtraverse(node->left, visit);
rtraverse(node->right, visit);
(*visit)(node);
}
/* iterative traversals */
void litraverse(btnode *root, void (*visit)(btnode*))
{
stack<node*> s; s.push(root);
btnode *n;
while (!s.empty()) {
(*visit)(n = s.top()); n.pop();
if (n->right) s.push(n->right);
if (n->left) s.push(n->left);
}
}
void citraverse(btnode *root, void (*visit)(btnode*))
{
stack<node*> s; s.push(root);
btnode *n;
while (!s.empty()) {
if (n->right) s.push(n->right);
(*visit)(n = s.top()); n.pop();
if (n->left) s.push(n->left);
}
}
void ritraverse(btnode *root, void (*visit)(btnode*))
{
stack<node*> s; s.push(root);
btnode *n;
while (!s.empty()) {
if (n->right) s.push(n->right);
if (n->left) s.push(n->left);
(*visit)(n = s.top()); n.pop();
}
}
void btraverse(btnode *root, void (*visit)(btnode*))
{
queue<node*> q; q.push(root);
btnode *n;
while (!q.empty()) {
(*visit)(n = q.front()); q.pop();
if (n->right) q.push(n->right);
if (n->left) q.push(n->left);
}
}
/* miscellaneous procedures */
int count(btnode *root)
{
if (!root) return 0;
return count(root->left) + count(root->right) + 1;
}
int height(btnode *root)
{
if (!root) return -1;
int u = height(root->left);
int v = height(root->right);
return u > v ? u + 1 : v + 1;
}
/* return container of tree's levels */
template <typename T>
static void levels_helper(btnode *root, vector<vector<T>>& res, int lvl)
{
if (!root) return;
if (res.size() == lvl) res.push_back({root->val});
else res[lvl].push_back(root->val);
levels_helper(root->left, res, lvl + 1);
levels_helper(root->right, res, lvl + 1);
}
template <typename T>
vector<vector<T>> levels(btnode *root)
{
vector<vector<T>> res;
if (!root) return res;
levels_helper(root, res, 0);
return res;
}
/***/
/* return container of tree's paths */
template <typename T>
static void paths_helper(btnode *root, vector<vector<T>>& res, vector<T>& cur)
{
cur.push_back(root->val);
if (!(root->left || root->right)) {
res.push_back(cur);
cur.pop();
return;
}
if (root->left) paths_helper(root->left, res, cur);
if (root->ritght) paths_helper(root->right, res, cur);
cur.pop();
}
template <typename T>
vector<vector<T>> paths(btnode *root)
{
vector<vector<T>> res;
if (!root) return res;
paths_helper(root, res, cur);
return res;
}
/***/
/* determine if the tree has a root-to-leaf path with specified sum */
template <typename T>
static bool godeeper(btnode *root, T sum, T cur)
{
if (!root) return false;
if (!(root->left || root->right))
return (cur + root->val) == sum;
return godeeper(root->left, sum, cur + root->val) ||
godeeper(root->right, sum, cur + root->val);
}
bool haspathsum(btnode *root, T sum)
{
return godeeper(root, sum, 0);
}
/***/