-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_utils.cpp
164 lines (133 loc) · 4.42 KB
/
tree_utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int value): val(value), left(NULL), right(NULL) {};
};
class TreesUtils {
/*
** In case of linked list and trees pointer to pointer has to be sent
** in questions where the the sent head has to point somewhere else
** i.e VALUE IN POINTER HAS TO BE CHANGED SO POINTER TO POINTER HAS TO BE SENT
*/
/*
** Balanced Binary tree is one which has all the levels filled except maybe the last one
** and all the nodes are possiblly to the leftmost.
** Full binary tree is one in which every node has either 0 or 2 childeren
*/
private:
/*
**traversals
*/
//inorder
void recurInorder(TreeNode* root, vector<int>& res) {
if (root == NULL) {
return;
}
recurInorder(root->left, res);
res.push_back(root->val);
recurInorder(root->right, res);
}
void iterativeInorder(TreeNode* root, vector<int>& res) {
if (root == NULL) {
return;
}
stack<TreeNode*> st;
TreeNode* curr = root;
while (curr != NULL || !st.empty()) {
while (curr != NULL) {
st.push(curr);
curr = curr->left;
}
curr = st.top();
st.pop();
res.push_back(curr->val);
curr = curr->right;
}
}
//preorder
void recurPreorder(TreeNode* root, vector<int>& res) {
if (root == NULL) {
return;
}
res.push_back(root->val);
recurPreorder(root->left, res);
recurPreorder(root->right, res);
}
void iterativePreorder(TreeNode* root, vector<int>& res) {
if (root == NULL) {
return;
}
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode *curr = st.top();
st.pop();
res.push_back(curr->val);
if (curr->right != NULL) {
st.push(curr->right);
}
if (curr->left != NULL) {
st.push(curr->left);
}
}
}
/* postorder */
void recurPostorder(TreeNode* root, vector<int>& res) {
if (root == NULL) {
return;
}
recurPostorder(root->left, res);
recurPostorder(root->right, res);
res.push_back(root->val);
}
/* -----> have not been able to do a iterative one for post order <------ */
/*
** if simple level order, just push in one order
*/
void recurZigZagLevelOrder(TreeNode* root, int cur, int height, vector<vector<int>>& res) {
if (root == NULL) {
return;
}
if (cur < height) {
if (height % 2 == 0) {
recurZigZagLevelOrder(root->left, cur+1, height, res);
recurZigZagLevelOrder(root->right, cur+1, height, res);
} else {
recurZigZagLevelOrder(root->right, cur+1, height, res);
recurZigZagLevelOrder(root->left, cur+1, height, res);
}
return;
}
if (res.size() <= height) {
res.push_back(vector<int>());
}
res[height].push_back(root->val);
return;
}
public:
static vector<int> traverseTree(TreeNode* root) {
vector<int> res;
/*
** methods among the above ^^
** typeOrderOfChoiceFn(root, res);
*/
return res;
}
static int getHeight(TreeNode* root) {
if(root == NULL)
return 0;
return 1+ max(getHeight(root->left),getHeight(root->right));
}
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
int height = getHeight(root);
int h = 0;
while (h < height) {
recurZigZagLevelOrder(root, 0, h, res);
h++;
}
return res;
}
};