-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.cpp
262 lines (230 loc) · 4.34 KB
/
binary_tree.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
class node{
public:
int data;
node*left;
node*right;
node(int d):data(d),left(NULL),right(NULL){}
};
node*buildTree(){
int d;
cin>>d;
if(d==-1){
return NULL;
}
node*n = new node(d);
n->left = buildTree();
n->right = buildTree();
return n;
}
void printTree(node*root){
if(root==NULL){
return;
}
cout<<root->data<<" ";
printTree(root->left);
printTree(root->right);
}
void printTreeIn(node*root){
if(root==NULL){
return;
}
printTreeIn(root->left);
cout<<root->data<<" ";
printTreeIn(root->right);
}
void printTreePost(node*root){
if(root==NULL){
return;
}
printTreePost(root->left);
printTreePost(root->right);
cout<<root->data<<" ";
}
void printAtLevelK(node*root,int k){
if(root==NULL||k<1){
return;
}
if(k==1){
cout<<root->data<<" ";
return;
}
printAtLevelK(root->left,k-1);
printAtLevelK(root->right,k-1);
}
int height(node*root){
if(root==NULL){
return 0;
}
int h1 = height(root->left);
int h2 = height(root->right);
return max(h1,h2)+1;
}
void printAllLevels(node*root){
int maxLevel = height(root);
for(int k=1;k<=maxLevel;k++){
printAtLevelK(root,k);
cout<<endl;
}
return;
}
//Level Order Build - Input
node* buildLevelOrder(){
cout<<"Enter root data";
int d;
cin>>d;
node*root = new node(d);
queue<node*> q;
q.push(root);
while(!q.empty()){
node*n = q.front();
q.pop();
cout<<"Enter children of "<<n->data<<" ";
int c1,c2;
cin>>c1>>c2;
if(c1!=-1){
n->left = new node(c1);
q.push(n->left);
}
if(c2!=-1){
n->right = new node(c2);
q.push(n->right);
}
}
return root;
}
//Breadth First Traversal | level order iterative
void levelOrder(node *root){
queue<node*> q;
q.push(root);
q.push(NULL);
while(!q.empty()){
node* n = q.front();
if(n==NULL){
cout<<endl;
q.pop();
if(!q.empty()){
q.push(NULL);
}
}
else{
q.pop();
cout<<n->data<<" ";
if(n->left){
q.push(n->left);
}
if(n->right){
q.push(n->right);
}
}
}
}
int replaceSum(node*root){
//Base Case
if(root==NULL){
return 0;
}
if(root->left==NULL && root->right==NULL){
return root->data;
}
int temp = root->data;
int leftSum = replaceSum(root->left);
int rightSum = replaceSum(root->right);
root->data = leftSum + rightSum;
return temp + root->data;
}
void mirror(node *root){
if(root==NULL){
return;
}
swap(root->left,root->right);
mirror(root->left);
mirror(root->right);
}
//You have to check if a tree is height balanced
class Pair{
public:
bool balance;
int height;
};
Pair isHeightBalanced(node*root){
Pair p;
if(root==NULL){
p.balance = true;
p.height = 0;
return p;
}
//Rec Case
Pair left = isHeightBalanced(root->left);
Pair right = isHeightBalanced(root->right);
if(left.balance && right.balance && abs(left.height-right.height)<=1)
p.balance = true;
else
p.balance = false;
p.height = max(left.height,right.height) +1;
return p;
}
pair<int,int> maxSubset(node*root){
if(root==NULL){
return make_pair(0,0);
}
pair<int,int> p,left,right;
//Postorder (bottom up way)
left = maxSubset(root->left);
right = maxSubset(root->right);
p.first = root->data + left.second + right.second;
p.second = max(left.first,left.second) + max(right.first,right.second);
return p;
}
pair<int,int> diameter(node*root){
if(root==NULL){
return make_pair(0,0);
}
pair<int,int> p,left,right;
left = diameter(root->left);
right = diameter(root->right);
p.first = max(left.first,right.first)+1;
p.second = max(left.second,max(right.second,left.first+right.first));
return p;
}
void printRoot2Leaf(node*root,int output[],int j){
//Base Case
if(root==NULL){
return;
}
if(root->left==NULL && root->right==NULL){
//Print the current output
output[j] = root->data;
for(int i=0;i<=j;i++){
cout<<output[i]<<",";
}
cout<<endl;
return;
}
//Rec Case
output[j] = root->data;
printRoot2Leaf(root->left,output,j+1);
printRoot2Leaf(root->right,output,j+1);
return;
}
int main(){
node*root = buildLevelOrder();//buildTree();
/*
printTree(root);
cout<<endl;
printTreeIn(root);
cout<<endl;
printTreePost(root);*/
//cout<<height(root);
//printAtLevelK(root,3);
//printAllLevels(root);
//levelOrder(root);
//pair<int,int> ans = maxSubset(root);
//cout<<max(ans.first,ans.second)<<endl;
int output[100] = {0};
printRoot2Leaf(root,output,0);
return 0;
}