forked from 7harshit20/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.cpp
733 lines (680 loc) · 21 KB
/
binaryTree.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
struct TreeNode {
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int num) {
val=num;
left=NULL;
right=NULL;
}
};
void preOrder(TreeNode* root) {
if(root==nullptr)return;
cout<<root->val<<" ";
preOrder(root->left);
preOrder(root->right);
}
void levelOrder(TreeNode* root, queue<TreeNode*>& add, vector<vector<int>>& sol) {
int n=add.size();
vector<int> ans;
for(int i=0;i<n;i++) {
if(add.front()->left)add.push(add.front()->left);
if(add.front()->right)add.push(add.front()->right);
ans.push_back(add.front()->val);
add.pop();
}
sol.push_back(ans);
if(!add.empty())levelOrder(add.front(), add, sol);
}
void levelOrder(TreeNode* root, vector<TreeNode*>& add, vector<int>& ans, vector<vector<int>>& sol, int i, vector<int>& level, int curr) {
if(level[i]>curr) {
sol.push_back(ans);
ans.clear();
curr++;
}
if(root->left) {
ans.push_back(root->left->val);
add.push_back(root->left);
level.push_back(curr+1);
}
if(root->right) {
ans.push_back(root->right->val);
add.push_back(root->right);
level.push_back(curr+1);
}
if(i+1<add.size())levelOrder(add[i+1], add, ans, sol, i+1, level, curr);
}
int maxDepth(TreeNode* root) {
if(!root)return 0;
return 1+max(maxDepth(root->left), maxDepth(root->right));
}
bool isBalanced(TreeNode* root) {
// Remember I solved it by 4 method, first in O(n*2) is for this we iterate through every single node and find the height of its left and right subtrees, which is not efficient. In my second approach I created a new tree with the value of node as their length (The idea is that while finding height of a root, we find all the height of its subtrees, so the value is updated in only one iteration i.e., when finding height of the top root). In third I used same approach, just didn't created a new tree but simply updated the same given tree(which is easier). In the last approach I did not updated the value of node as length, but returned it instead (code is in leetcode).
// O(N*2);
// if(!root)return true;
// if(abs(maxDepth(root->left)-maxDepth(root->right))>1)return false;
// return (isBalanced(root->left) && isBalanced(root->right));
// O(N)
// if(root->val==0)return true;
// if(abs(root->left->val-root->right->val)>1)return false;
// return (isBalanced(root->left) && isBalanced(root->right));
// O(N)
if(!root)return true;
if(abs(root->left->val-root->right->val)>1)return false;
return (isBalanced(root->left) && isBalanced(root->right));
}
int heigthTree(TreeNode* root, TreeNode*& num) {
num=new TreeNode(0);
if(!root) return 0;
num->val=1+max(heigthTree(root->left, num->left), heigthTree(root->right, num->right));
return num->val;
}
int findHeight(TreeNode* root) {
if(!root)return 0;
root->val=1+max(findHeight(root->left), findHeight(root->right));
return root->val;
}
int diameter(TreeNode* root, int& ans) {
if(!root)return 0;
int leftMax=diameter(root->left, ans);
int rightMax=diameter(root->right, ans);
ans=max(ans, leftMax+rightMax);
return 1+max(leftMax, rightMax);
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(!root)return false;
if(!root->left && !root->right)return targetSum==root->val;
if(!root->left)return hasPathSum(root->right, targetSum-root->val);
if(!root->right)return hasPathSum(root->left, targetSum-root->val);
return hasPathSum(root->left, targetSum-root->val) || hasPathSum(root->right, targetSum-root->val);
}
int mxPathSum(TreeNode* root, int& sum) {
if(!root)return 0;
int l=mxPathSum(root->left, sum);
int r=mxPathSum(root->right, sum);
sum=max(sum, max(l, 0)+max(r, 0)+root->val);
return max(max(l, r), 0)+root->val;
}
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q)return true;
if(p&&q) {
if(p->val==q->val)return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
return false;
}
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> sol;
if(!root)return sol;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int n=q.size();
vector<int> ans(n, 0);
for(int i=0;i<n;i++) {
TreeNode* node=q.front();
q.pop();
if(sol.size()%2==1)ans[n-1-i]=node->val;
else ans[i]=node->val;
if(node->left)q.push(node->left);
if(node->right)q.push(node->right);
}
sol.push_back(ans);
}
return sol;
}
void leftNodes(TreeNode* root, vector<int>& ans) {
root=root->left;
while(root) {
if(root->left|| root->right)ans.push_back(root->val);
if(root->left) root=root->left;
else root=root->right;
}
}
void rightNodes(TreeNode* root, vector<int>& ans) {
vector<int> temp;
root=root->right;
while(root) {
if(root->left || root->right)temp.push_back(root->val);
if(root->right) root=root->right;
else root=root->left;
}
int n=temp.size();
for(int i=0;i<n;i++) ans.push_back(temp[n-1-i]);
}
void leafNodes(TreeNode* root, vector<int>& ans) {
if(!root->left && !root->right)ans.push_back(root->val);
if(root->left)leafNodes(root->left, ans);
if(root->right)leafNodes(root->right, ans);
}
vector<int> boundaryTraversal(TreeNode* root) {
vector<int> ans;
if(!root)return ans;
if(root->left || root->right)ans.push_back(root->val);
leftNodes(root, ans);
leafNodes(root, ans);
rightNodes(root, ans);
return ans;
}
void verticalTraversalSol(TreeNode* root, int x, int y, vector<vector<pair<int, int>>>& sol) {
if(!root)return;
sol[x].push_back({ root->val, y });
verticalTraversalSol(root->left, x-1, y+1, sol);
verticalTraversalSol(root->right, x+1, y+1, sol);
}
bool mycmp(pair<int, int> p1, pair<int, int> p2) {
if(p1.second==p2.second)return p1.first<p2.first;
return p1.second<p2.second;
}
vector<vector<int>> verticalTraversal(TreeNode* root) {
int n=7;
vector<vector<int>> finalSol;
vector<vector<pair<int, int>>> sol(15, { {} });
verticalTraversalSol(root, n, 0, sol);
for(auto& ele: sol) {
vector<int> temp;
int size=ele.size();
if(size==1)continue;
sort(ele.begin(), ele.end(), mycmp);
for(int i=1;i<size;i++) temp.push_back(ele[i].first);
finalSol.push_back(temp);
};
return finalSol;
}
void rightView(TreeNode* root, int level, vector<int>& ans) {
if(!root)return;
if(ans.size()==level)ans.push_back(root->val);
rightView(root->right, level+1, ans);
rightView(root->left, level+1, ans);
}
void bottomLeft(TreeNode* root, int level, pair<int, int>& ans) {
if(!root)return;
if(level>=ans.second)ans={ root->val,level };
bottomLeft(root->right, level+1, ans);
bottomLeft(root->left, level+1, ans);
}
void topSideView(TreeNode* root, int x, int y, vector<pair<int, int>>& ans) {
if(!root)return;
if(ans[x].first==0 || ans[x].second>y)ans[x]={ root->val,y };
topSideView(root->right, x+1, y+1, ans);
topSideView(root->left, x-1, y+1, ans);
}
vector<int> topView(TreeNode* root) {
int n=1000000;
vector<int> sol;
vector<pair<int, int>> ans(n, { 0,0 });
topSideView(root, n/2, 0, ans);
for(int i=0;i<n;i++)if(ans[i].first!=0)sol.push_back(ans[i].first);
return sol;
}
bool checkSym(TreeNode* root1, TreeNode* root2) {
if(!root1 || !root2)return root1==root2;
if(root1->val!=root2->val)return false;
return checkSym(root1->left, root2->right) && checkSym(root1->right, root2->left);
}
void pathToLeaf(TreeNode* root, vector<int>& ans, vector<vector<int>>& sol) {
if(!root)return;
ans.push_back(root->val);
if(!root->left && !root->right)sol.push_back(ans);
pathToLeaf(root->left, ans, sol);
pathToLeaf(root->right, ans, sol);
ans.pop_back();
}
bool pathToNode(TreeNode* root, vector<int>& ans, TreeNode* target) {
if(!root)return false;
ans.push_back(root->val);
if(root==target)return true;
if(pathToNode(root->left, ans, target) || pathToNode(root->right, ans, target))return true;
ans.pop_back();
return false;
}
bool lca(TreeNode* root, TreeNode* p, TreeNode* q, TreeNode*& ans) {
if(ans||!root)return false;
bool i=(root==p || root==q);
bool l=lca(root->left, p, q, ans);
bool r=lca(root->right, p, q, ans);
if((l&&r)||(l&&i)||(r&&i))ans=root;
return i||l||r;
}
int widthOfBinaryTree(TreeNode* root) {
int ans=1;
queue<pair<TreeNode*, int>> q;
q.push({ root,0 });
while(!q.empty()) {
int n=q.size(), s, e;
int first=-1;
for(int i=1;i<=n;i++) {
TreeNode* node=q.front().first;
int x=q.front().second;
if(i==1)s=x;
if(i==n)e=x;
if(first==-1 && (node->left || node->right))first=q.front().second;
if(node->left) q.push({ node->left,(x-first)*2 });
if(node->right) q.push({ node->right,(x-first)*2+1 });
q.pop();
}
ans=max(ans, e-s+1);
}
return ans;
}
void kDown(TreeNode* root, int k, vector<int>& sol) {
if(k<0)return;
if(k==0) {
sol.push_back(root->val);
return;
}
if(root->left)kDown(root->left, k-1, sol);
if(root->right)kDown(root->right, k-1, sol);
}
bool pathToNodeK(TreeNode* root, vector<TreeNode*>& ans, TreeNode* target) {
if(!root)return false;
ans.push_back(root);
if(root==target || pathToNodeK(root->left, ans, target) || pathToNodeK(root->right, ans, target))return true;
ans.pop_back();
return false;
}
int timeDown(TreeNode* root) {
int lt=0, rt=0;
if(!root)return 0;
if(root->left)lt=timeDown(root->left);
if(root->right)rt=timeDown(root->right);
return 1+max(lt, rt);
}
int totalNodes(TreeNode* root) {
// Approach when all leaf nodes are not as far left as possible
queue<TreeNode*> q;
q.push(root);
int found=0, num=0, level=1;
while(!found) {
int n=q.size();
level++;
for(int i=0;i<n;i++) {
TreeNode* node=q.front();
if(!node->left) {
num++;
found=1;
}
else q.push(node->left);
if(!node->right) {
num++;
found=1;
}
else q.push(node->right);
}
}
return pow(2, level)-num;
}
int leftHeight(TreeNode* root) {
if(!root)return 0;
return 1+leftHeight(root->left);
}
int rightHeight(TreeNode* root) {
if(!root)return 0;
return 1+rightHeight(root->right);
}
int countNodes(TreeNode* root) {
if(!root)return 0;
int lh=leftHeight(root->left);
int rh=rightHeight(root->right);
if(lh==rh)return pow(2, lh) - 1;
return 1+countNodes(root->left)+countNodes(root->right);
}
TreeNode* preInTree(vector<int>& preorder, vector<int>& inorder, int& currRoot, int srootPos, int erootPos) {
if(srootPos>erootPos)return nullptr;
TreeNode* root=new TreeNode(preorder[currRoot]);
int nrootPos=find(inorder.begin(), inorder.end(), preorder[currRoot]) - inorder.begin();
currRoot++;
root->left=preInTree(preorder, inorder, currRoot, srootPos, nrootPos-1);
root->right=preInTree(preorder, inorder, currRoot, nrootPos+1, erootPos);
return root;
}
TreeNode* postInTree(vector<int>& inorder, vector<int>& postorder, int& currRoot, int srootPos, int erootPos) {
if(srootPos>erootPos)return nullptr;
TreeNode* root=new TreeNode(postorder[currRoot]);
int nrootPos=find(inorder.begin(), inorder.end(), postorder[currRoot])-inorder.begin();
currRoot--;
root->right=postInTree(inorder, postorder, currRoot, nrootPos+1, erootPos);
root->left=postInTree(inorder, postorder, currRoot, srootPos, nrootPos-1);
return root;
}
string serialize(TreeNode* root) {
queue<TreeNode*> q;
string s="";
if(!root)return s;
q.push(root);
s+=to_string(root->val)+",";
while(!q.empty()) {
int n=q.size();
for(int i=0;i<n;i++) {
TreeNode* node=q.front();
if(node->left) {
q.push(node->left);
s+=to_string(node->left->val)+",";
}
else s+="#,";
if(node->right) {
q.push(node->right);
s+=to_string(node->right->val)+",";
}
else s+="#,";
q.pop();
}
}
s.pop_back();
return s;
}
TreeNode* deserialize(string s) {
// TreeNode* deserialize(string s) {
// if(s.length()==0)return nullptr;
// stringstream ss(s);
// queue<TreeNode*> q;
// string str;
// getline(ss, str, ',');
// TreeNode* root=new TreeNode(stoi(str));
// q.push(root);
// while(!q.empty()) {
// TreeNode* node=q.front();
// getline(ss, str, ',');
// if(str!="#") {
// node->left=new TreeNode(stoi(str));
// q.push(node->left);
// }
// getline(ss, str, ',');
// if(str!="#") {
// node->right=new TreeNode(stoi(str));
// q.push(node->right);
// }
// q.pop();
// }
// return root;
// }
vector<string> arr;
int c=0, len=s.length();
for(int i=0;i<len;i++) {
if(s[i]==',') {
arr.push_back(s.substr(c, i-c));
c=i+1;
}
else if(i==len-1) {
arr.push_back(s.substr(c, len-c));
}
}
queue<TreeNode*> q;
if(len==0)return nullptr;
TreeNode* root=new TreeNode(stoi(arr[0]));
q.push(root);
int n=arr.size();
for(int i=1;i<n&&!q.empty();i+=2) {
TreeNode* node=q.front();
try {
node->left=(new TreeNode(stoi(arr[i])));
q.push(node->left);
}
catch(const exception& e) {}
try {
node->right=(new TreeNode(stoi(arr[i+1])));
q.push(node->right);
}
catch(const exception& e) {}
q.pop();
}
return root;
}
TreeNode* flattenHelper(TreeNode* root) {
if(!root)return nullptr;
TreeNode* l=flattenHelper(root->left);
TreeNode* r=flattenHelper(root->right);
if(!l&&!r)return root;
if(!l)return r;
TreeNode* currRight=root->right;
root->right=root->left;
l->right=currRight;
root->left=nullptr;
if(r)return r;
return l;
}
void flatten(TreeNode* root) {
// Recursive way
// flattenHelper(root);
// constant space
TreeNode* temp=root;
while(root) {
if(!root->left) {
root=root->right;
continue;
}
temp=root->left;
while(temp->right!=nullptr && temp->right!=root)temp=temp->right;
if(!temp->right) {
temp->right=root;
root=root->left;
}
else {
temp->right=root->right;
root->right=root->left;
root->left=nullptr;
root=temp->right;
}
}
}
vector<int> morrisTraversalInorder(TreeNode* root) {
vector<int> ans;
TreeNode* temp=root;
while(root) {
if(!root->left) {
ans.push_back(root->val);
root=root->right;
continue;
}
temp=root->left;
while(temp->right!=nullptr && temp->right!=root) {
temp=temp->right;
}
if(temp->right==nullptr) {
temp->right=root;
root=root->left;
}
else {
temp->right=nullptr;
ans.push_back(root->val);
root=root->right;
}
}
return ans;
}
vector<int> morrisTraversalPreorder(TreeNode* root) {
vector<int> ans;
TreeNode* temp=root;
while(root) {
if(!root->left) {
ans.push_back(root->val);
root=root->right;
continue;
}
temp=root->left;
while(temp->right!=nullptr && temp->right!=root) {
temp=temp->right;
}
if(temp->right==nullptr) {
temp->right=root;
ans.push_back(root->val);
root=root->left;
}
else {
temp->right=nullptr;
root=root->right;
}
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
TreeNode* root=new TreeNode(1);
root->left=new TreeNode(2);
root->right=new TreeNode(3);
root->left->left=new TreeNode(4);
root->left->right=new TreeNode(5);
root->right->left=new TreeNode(6);
root->right->right=new TreeNode(7);
root->left->right->left=new TreeNode(8);
root->left->right->right=new TreeNode(9);
root->left->right->right->left=new TreeNode(10);
// vector<vector<int>> sol;
// queue<TreeNode*> add;
// add.push(root);
// levelOrder(root, add, sol);
// Iterative PreOrder
// vector<int> ans;
// stack<TreeNode*> st;
// TreeNode* head=root;
// do {
// if(!head) {
// head=st.top();
// st.pop();
// }
// ans.push_back(head->val);
// if(head->right)st.push(head->right);
// head=head->left;
// } while(!st.empty() || head);
// for(auto ele: ans)cout<<ele<<" ";
// Iterative InOrder
// vector<int> ans;
// stack<TreeNode*> st;
// while(!st.empty()||root) {
// if(!root) {
// root=st.top()->right;
// ans.push_back(st.top()->val);
// st.pop();
// }
// else{
// st.push(root);
// root=root->left;
// }
// }
// for(auto ele: ans)cout<<ele<<" ";
// Iterative PostOrder
// vector<int> ans;
// stack<TreeNode*> st;
// stack<int> check;
// while(!st.empty()||root) {
// if(!root) {
// while(!check.empty() && check.top()==1) {
// ans.push_back(st.top()->val);
// st.pop();
// check.pop();
// }
// if(!st.empty()) {
// root=st.top()->right;
// check.top()=1;
// }
// }
// if(root) {
// st.push(root);
// check.push(0);
// root=root->left;
// }
// }
// for(auto ele: ans)cout<<ele<<" ";
// vector<int> ans;
// stack<TreeNode*> st;
// TreeNode* pre;
// TreeNode* ob=new TreeNode(0);
// while(!st.empty()||root) {
// if(!root) {
// TreeNode* node=st.top();
// if(node==pre || node->left==pre) {
// root=node->right;
// pre= ob;
// }
// else {
// ans.push_back(node->val);
// pre=node;
// st.pop();
// }
// }
// else {
// st.push(root);
// pre=root;
// root=root->left;
// }
// }
// for(auto ele: ans)cout<<ele<<" ";
// Pre,In,Post in one iteration
// vector<int> pre, in, post;
// stack<pair<TreeNode*, int>> st;
// while(!st.empty()|| root) {
// if(!root) {
// auto& ele=st.top();
// if(ele.second==0) {
// in.push_back(ele.first->val);
// ele.second=1;
// root=ele.first->right;
// }
// else {
// post.push_back(ele.first->val);
// st.pop();
// }
// }
// else {
// pre.push_back(root->val);
// st.push({ root,0 });
// root=root->left;
// }
// }
// for(auto ele: pre)cout<<ele<<" ";
// cout<<endl;
// for(auto ele: in)cout<<ele<<" ";
// cout<<endl;
// for(auto ele: post)cout<<ele<<" ";
// cout<<endl;
// cout<<maxDepth(root)<<endl;
// TreeNode* num=NULL;
// heigthTree(root, num);
// int ans=0;
// diameter(root, ans);
// cout<<ans;
// int k=2;
// vector<TreeNode*> ans;
// vector<int> sol;
// pathToNodeK(root, ans, root->left);
// int n=ans.size();
// for(int i=0;i<n-1;i++) {
// if(ans[i]->left==ans[i+1] && ans[i]->right) kDown(ans[i]->right, k+i-n, sol);
// if(ans[i]->right==ans[i+1] && ans[i]->left) kDown(ans[i]->left, k+i-n, sol);
// }
// if(k<=n-1 && k!=0)sol.push_back(ans[n-1-k]->val);
// kDown(ans[n-1], k, sol);
// for(auto ele: sol) {
// cout<<ele<<" ";
// }
// TreeNode* target=root->left->right->left;
// vector<TreeNode*> ans;
// pathToNodeK(root, ans, target);
// int n=ans.size(), sol=0;
// for(int i=0;i<n-1;i++) {
// if(ans[i]->left==ans[i+1]) {
// sol=max(sol, n-1-i+timeDown(ans[i]->right));
// }
// if(ans[i]->right==ans[i+1]) {
// sol=max(sol, n-1-i+timeDown(ans[i]->left));
// }
// }
// sol=max(sol, timeDown(target)-0);
// cout<<sol;
// flattenHelper(root);
// preOrder(root);
// vector<int> ans=morrisTraversalPreorder(root);
// for(auto ele: ans)cout<<ele<<" ";
return 0;
}