-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
423 lines (362 loc) · 10.1 KB
/
test.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include<bits/stdc++.h>
using namespace std;
class node{ //data structure for binary tree
public:
int data;
int hd;//horizontal distance i.e., distance from root
node* left;
node* right;
node(int value){
//constructor
data = value;
left = NULL;
right = NULL;
hd = INT_MAX;
}
node(){
data = INT_MAX;
hd = INT_MAX;
left = NULL;
right = NULL;
}
};
int height(node* root){
if(root == NULL)
return 0;
int leftHeight = 1 + height(root->left);
int rightHeight = 1 + height(root->right);
return max(leftHeight,rightHeight);
}
//similarly you can write recursively for other traversals
void preorder_recursive(node* root){
if(root == NULL)
return;
cout<<root->data<<" ";
preorder_recursive(root->left);
preorder_recursive(root->right);
return;
}
void preorder_iterative(node* root){
node* temp = root;
stack<node*> s;
//s.push(root);
while(true){
while(temp!=NULL){
cout<<temp->data<<" ";
s.push(temp);
temp = temp->left;
}
if(s.empty())
break;
temp = s.top();
s.pop();
temp = temp->right;
}
return;
}
void inorder_iterative(node* root){
node* temp = root;
stack<node*> s;
while(true){
while(temp != NULL){
s.push(temp);
temp = temp->left;
}
if(s.empty())
break;
temp = s.top();
s.pop();
cout<<temp->data<<" ";
temp = temp->right;
}
}
//bfs
void levelorder(node* root){
node* temp = root;
queue<node*> q;
q.push(temp);
while(!q.empty()){
temp = q.front();
q.pop();
cout<<temp->data<<" ";
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
}
void postorder_iterative(node* root){
node* temp = root;
node* prev = NULL;
stack<node*> s;
do{
while(temp != NULL){
s.push(temp);
temp = temp->left;
}
while(temp == NULL && !s.empty()){
temp = s.top();
//ya to leaf node ho aur ya toh right already processed h tab hum print krege
if(temp->right == NULL || temp->right == prev){
cout<<temp->data<<" ";
s.pop();
prev = temp;
temp = NULL;
}
//this means ki left process hogya h but right nahi hua h
else{
temp = temp->right;
}
}
}while(!s.empty());
}
//assuming k1 and k2 exists in the tree
node* lca(node* root,int k1,int k2){
if(root == NULL)
return NULL;
//agar aisa h then root hi lca hua
if(root->data == k1 || root->data == k2)
return root;
//otherwise left pe call kro and right pe call kro
node* lca_left = lca(root->left,k1,k2);
node* lca_right = lca(root->right,k1,k2);
//means ek left m h and ek right m h
if(lca_left!=NULL && lca_right!=NULL){
return root;
}
return (lca_left==NULL)?lca_right:lca_left;
}
// left and Right view
//basically left aur rightview ko hume level ke according krna h,har level ka pehla node while calling left or right first,recursively
void leftView(node* root,int curr_level,int &max_level_so_far){
if(root == NULL)
return;
//basically we need to print first node of every level while calling left first
if(curr_level > max_level_so_far){
cout<<root->data<<endl;
max_level_so_far = curr_level;
}
leftView(root->left,curr_level+1,max_level_so_far);
leftView(root->right,curr_level+1,max_level_so_far);
}
void rightView(node* root,int curr_level,int &max_level_so_far){
if(root == NULL)
return;
//basically we need to print first node of every level while calling right first
if(curr_level > max_level_so_far){
cout<<root->data<<endl;
max_level_so_far = curr_level;
}
rightView(root->right,curr_level+1,max_level_so_far);
rightView(root->left,curr_level+1,max_level_so_far);
}
//Top and Bottom View
void topView(node* root){
node* temp = root;
int hd = 0;
temp->hd = 0; // root ka hd 0 krdo
map<int,int> m; // hd ke corresponding temp->data rakhege
queue<node*> q;
q.push(temp);
while(!q.empty()){
temp = q.front();
q.pop();
hd = temp->hd;
//means ki ye vala hd map m nahi aaya h
if(m.find(hd) == m.end()){
m[hd] = temp->data;
}
if(temp->left){
temp->left->hd = hd-1;
q.push(temp->left);
}
if(temp->right){
temp->right->hd = hd+1;
q.push(temp->right);
}
}
//at this point our ans is in the map
for(auto i=m.begin();i!=m.end();i++){
cout<<i->second<<" ";
}
}
void bottomView(node* root){
node* temp = root;
int hd = 0;
temp->hd = 0; // root ka hd 0 krdo
map<int,int> m; // hd ke corresponding temp->data rakhege
queue<node*> q;
q.push(temp);
while(!q.empty()){
temp = q.front();
q.pop();
hd = temp->hd;
//har baar update krte raho , jo last update hoga vhi bottom view ka part hoga
m[hd] = temp->data;
if(temp->left){
temp->left->hd = hd-1;
q.push(temp->left);
}
if(temp->right){
temp->right->hd = hd+1;
q.push(temp->right);
}
}
//at this point our ans is in the map
for(auto i=m.begin();i!=m.end();i++){
cout<<i->second<<" ";
}
}
//max distance b/w any two leaf nodes
//max(passing thru root,diameter of leftsubtree,diameter of right subtree)
int diameter(node* root){
if(root == NULL)
return 0;
int lh=height(root->left);
int rh=height(root->right);
int ld=diameter(root->left);
int rd=diameter(root->right);
return max(lh+rh+1,max(ld,rd));
}
//absolute difference of lheight and rheight for every node
bool isheightBalanced(node* root){
if(root == NULL)
return true;
int lh = height(root->left);
int rh = height(root->right);
if(abs(lh-rh) <= 1 && isheightBalanced(root->left) && isheightBalanced(root->right)){
return true;
}
return false;
}
void printLevel(node* root,int level){
if(root == NULL)
return ;
if(level == 1)
cout<<root->data;
else{
printLevel(root->left,level-1);
printLevel(root->right,level-1);
}
}
void mirrorify(node* root,node** mirror){
if(root==NULL)
return;
*mirror = new node(root->data);
mirrorify(root->left,&(*mirror)->right);
mirrorify(root->right,&(*mirror)->left);
}
int findIndex(string str,int si,int ei){
if(si > ei)
return -1;
stack<char> s;
for(int i=si;i<=ei;i++){
if(str[i] == '(')
s.push(str[i]);
else if(str[i] == ')'){
if(s.top() == '('){
s.pop();
//if at any iteration stack i empty then this is the required index
if(s.empty())
return i;
}
}
}
return -1;//not found
}
//starting index and ending index
node* buildTreeFromString(string str,int si,int ei){
if(si>ei)
return NULL;
node* root = new node(str[si] - '0');//interger m convert krne ke liye subtract
int index = -1;
if(si + 1 <= ei && str[si+1] == '(' ){
index = findIndex(str,si+1,ei);
}
//now index of starting ( is si+1 and ) is index variable (left subtree)
//index of starting ( is index+1 and ending ) is ei-1 (right subtree)
if(index != -1){
root->left = buildTreeFromString(str,si+2,index-1);
root->right = buildTreeFromString(str,index+2,ei-1);
}
return root;
}
void checkHelper(node* root,vector<int> &leaf_level,int level){
if(!root)
return;
if(!root->left && !root->right){
leaf_level.push_back(level);
return;
}
checkHelper(root->left,leaf_level,level+1);
checkHelper(root->right,leaf_level,level+1);
}
/*You are required to complete this method*/
bool check(node *root)
{
//Your code here
vector<int> leaf_level;
checkHelper(root,leaf_level,0);
for(int i=0;i<leaf_level.size();i++)
{
/*if(leaf_level[i] != leaf_level[i-1])
return false;*/
cout<<leaf_level[i]<<" ";
}
return true;
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
/* 1
2 3
4 5 6 7
8 9
10
*/
//make tree
node* root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);
root->right->left = new node(6);
root->right->right = new node(7);
root->left->left->left = new node(8);
root->left->left->right = new node(9);
root->left->left->right->left = new node(10);
//preorder_recursive(root);
/*cout<<endl;
preorder_iterative(root);
cout<<endl;
inorder_iterative(root);
cout<<endl;
postorder_iterative(root);
cout<<endl;
levelorder(root);
cout<<endl;
cout<<height(root);
cout<<endl;
node* ans = lca(root,5,3);
if(ans != NULL)
cout<<ans->data;
cout<<endl;*/
/*int max_level_so_far_1 = 0;
leftView(root,1,max_level_so_far_1);*/
/*int max_level_so_far_2 = 0;
rightView(root,1,max_level_so_far_2);*/
// topView(root);
// cout<<endl;
// bottomView(root);
/* node* rootMirror = NULL ;
mirrorify(root,&rootMirror);
inorder_iterative(root);
cout<<endl;
inorder_iterative(rootMirror);*/
string str = "4(2(3)(1))(6(5)(4))";
node* root_1 = buildTreeFromString(str, 0, str.length() - 1);
//preorder_iterative(root_1);
bool ans = check(root_1);
return 0;
}