-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedium-level-tree.cpp
More file actions
1867 lines (1612 loc) Β· 52.2 KB
/
medium-level-tree.cpp
File metadata and controls
1867 lines (1612 loc) Β· 52.2 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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// πΉ 1. Height of Tree
// π§ Mathematical Definition
// Height(T)=max(Height(left),Height(right))+1
// Base case:
// Height(NULL)=β1 (edge-based)or0 (node-based)
// πΉ 2. Level
// π§ Mathematical Definition
// Level(T)=max(Level(left),Level(right))+1
// Base case:
// Level(NULL)=0
// πΉ 3. Diameter of Tree
// π§ Mathematical Definition
// Diameter(T)=max(Diameter(left),Diameter(right),Height(left)+Height(right)+2)
// Base case:
// Diameter(NULL)=0
// πΉ 4. Maximum Path Sum in Binary Tree
// π§ Mathematical Definition
// MaxPathSum(T)=max(MaxPathSum(left),MaxPathSum(right),max(0,MaxPathSumFromNode(left))+max(0,MaxPathSumFromNode(right))+T.val)
// Base case:
// MaxPathSum(NULL)=0
// πΉ 5. Check if Tree is Balanced
// π§ Mathematical Definition
// A tree is balanced if for every node, the difference in height between its left and right subtrees is at most 1.
// Base case:
// A tree is balanced if for every node, the difference in height between its left and right subtrees is at most 1.
// πΉ 6. Check if Tree is Symmetric
// π§ Mathematical Definition
// A tree is symmetric if the left subtree is a mirror reflection of the right subtree.
// Base case:
// A tree is symmetric if the left subtree is a mirror reflection of the right subtree.
// πΉ 7. Binary Search (Recursion)
// π§ Mathematical Definition
// Binary Search(low,high):
// mid=low+(high-low)/2
// if arr[mid]==target return mid
// if target<arr[mid] return Binary Search(low,mid-1)
// if target>arr[mid] return Binary Search(mid+1,high)
// Base case:
// if low>high return -1 (target not found in the array)
// if arr[mid]==target return mid (target found at index mid)
// πΉ 8. Count Nodes in Binary Tree
// π§ Mathematical Definition
// CountNodes(T)=CountNodes(left)+CountNodes(right)+1
// Base case:
// CountNodes(NULL)=0
// πΉ 9. Count Leaf Nodes in Binary Tree
// π§ Mathematical Definition
// CountLeafNodes(T)=CountLeafNodes(left)+CountLeafNodes(right)
// Base case:
// CountLeafNodes(NULL)=0
// CountLeafNodes(leaf)=1
// πΉ 10. Count Non-Leaf Nodes in Binary Tree
// π§ Mathematical Definition
// CountNonLeafNodes(T)=CountNonLeafNodes(left)+CountNonLeafNodes(right)+1
// Base case:
// CountNonLeafNodes(NULL)=0
// CountNonLeafNodes(leaf)=0
// πΉ 11. Count Nodes at K-th Level in Binary Tree
// π§ Mathematical Definition
// CountNodesAtK(T,k)=CountNodesAtK(left,k-1)+CountNodesAtK(right,k-1)
// Base case:
// CountNodesAtK(NULL,k)=0
// CountNodesAtK(node,k)=1 if k==0 else 0
// πΉ 12. Count Nodes in Complete Binary Tree
// π§ Mathematical Definition
// CountNodes(T)=2^h-1 where h is the height of the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// πΉ 13. Count Nodes in Perfect Binary Tree
// π§ Mathematical Definition
// CountNodes(T)=2^(h+1)-1 where h is the height of the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// πΉ 14. Count Nodes in Full Binary Tree
// π§ Mathematical Definition
// CountNodes(T)=2*h+1 where h is the height of the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// πΉ 15. Count Nodes in Binary Tree with N Nodes
// π§ Mathematical Definition
// CountNodes(T)=N where N is the total number of nodes in the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// πΉ 16. Count Nodes in Binary Tree with L Leaves
// π§ Mathematical Definition
// CountNodes(T)=2*L-1 where L is the total number of leaf nodes in the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// πΉ 17. Count Nodes in Binary Tree with N Non-Leaf Nodes
// π§ Mathematical Definition
// CountNodes(T)=N where N is the total number of non-leaf nodes in the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=0
// πΉ 18. Count Nodes in Binary Tree with N Nodes at K-th Level
// π§ Mathematical Definition
// CountNodes(T,k)=N where N is the total number of nodes at k-th level in the tree
// Base case:
// CountNodes(NULL,k)=0
// CountNodes(node,k)=1 if k==0 else 0
// πΉ 19. Count Nodes in Binary Tree with N Nodes and L Leaves
// π§ Mathematical Definition
// CountNodes(T)=N where N is the total number of nodes in the tree and L is the total number of leaf nodes in the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// πΉ 20. Count Nodes in Binary Tree with N Nodes and L Non-Leaf Nodes
// π§ Mathematical Definition
// CountNodes(T)=N where N is the total number of nodes in the tree and L is the total number of non-leaf nodes in the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=0
// π³ 1. Deletion in Binary Tree (General Case)
// π§ Idea (Mathematical Thinking)
// To delete node with value π₯
// Find:
// Target node π
// Deepest node π·
// Replace: TβD
// Base case:
// When we have found the target node and performed the necessary replacement and deletion, we return the modified tree; otherwise, we continue to search for the target node in the left and right subtrees.
// πΉ C++ Example: Deletion in Binary Tree
// πΉ 1. Height of Tree
// π§ Mathematical Definition
// Height(T)=max(Height(left),Height(right))+1
// Base case:
// Height(NULL)=β1 (edge-based)or0 (node-based)
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int height(Node* root){
if(root==NULL) return -1;
int leftheight=height(root->left);
int rightheight=height(root->right);
return max(leftheight,rightheight)+1;
}
int main(){
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);
cout<<height(root)<<endl;
return 0;
}
// πΉ 2. Level
// π§ Mathematical Definition
// Level(T)=max(Level(left),Level(right))+1
// Base case:
// Level(NULL)=0
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int level(Node* root){
if(root==NULL) return 0;
int leftlevel=level(root->left);
int rightlevel=level(root->right);
return max(leftlevel,rightlevel)+1;
}
int main(){
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);
cout<<level(root)<<endl;// Output: 3
return 0;
}
// πΉ 3. Diameter of Tree
// π§ Mathematical Definition
// Diameter(T)=max(Diameter(left),Diameter(right),Height(left)+Height(right)+2)
// Base case:
// Diameter(NULL)=0
// π§ Why +2?
// Because:
// Diameter through root=left height+right height+2
// π Each edge contributes +1
// π Dry Run (Small Example)
// 1
// / \
// 2 3
// leftheight = 0
// rightheight = 0
// Diameter
// Diameter=0+0+2=2
// β Correct (edges: 2β1β3)
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int Diameter(Node* root,int &height){
if(root==NULL){
height=-1;
return 0;
}
int leftheight=0,rightheight=0;
int leftdiameter=Diameter(root->left,leftheight);
int rightdiameter=Diameter(root->right,rightheight);
height=max(leftheight,rightheight)+1;
return max({leftdiameter,rightdiameter,leftheight+rightheight+2});
}
int main(){
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);
int height=0;
cout<<Diameter(root,height)<<endl;//output=3
return 0;
}
// πΉ 4. Maximum Path Sum in Binary Tree
// π§ Mathematical Definition
// MaxPathSum(T)=max(MaxPathSum(left),MaxPathSum(right),max(0,MaxPathSumFromNode(left))+max(0,MaxPathSumFromNode(right))+T.val)
// Base case:
// MaxPathSum(NULL)=0
// π§ Why max(0,...)?
// Because we want to maximize the path sum, and if the sum of a subtree is negative, we might as well ignore it and start a new path from the current node.
// π Dry Run (Small Example)
// 1
// / \
// 2 3
// MaxPathSumFromNode(2)=2
// MaxPathSumFromNode(3)=3
// MaxPathSum(1)=max(2,3,2+3+1)=6
// β Correct (path: 2β1β3)
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to calculate the maximum path sum.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Maximum Path Sum in Binary Tree
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int MaxPathSumFromNode(Node* root,int &maxsum){
if(root==NULL) return 0;
int leftsum=max(0,(MaxPathSumFromNode(root->left,maxsum)));
int rightsum=max(0,(MaxPathSumFromNode(root->right,maxsum)));
int currentmax=leftsum+rightsum+root->val;
maxsum=max(maxsum,currentmax);
return max(leftsum,rightsum)+root->val;
}
int MaxPathSum(Node* root){
int maxsum=INT_MIN;
MaxPathSumFromNode(root,maxsum);
return maxsum;
}
int main(){
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);
cout<<MaxPathSum(root)<<endl;//output=11 (path: 4β2β1β3)
return 0;
}
// πΉ 5. Check if Tree is Balanced
// π§ Mathematical Definition
// A tree is balanced if for every node, the difference in height between its left and right subtrees is at most 1.
// Base case:
// A tree is balanced if for every node, the difference in height between its left and right subtrees is at most 1.
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to check if it is balanced and to calculate its height.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Check if Tree is Balanced
// β
Your Logic (Mathematical View)
// You are using:
// Height (T)=max(Height(left),Height(right))+1 and if unbalanced return -1
// And condition: if(abs(leftheight-rightheight)<=1) then balanced else unbalanced
// π Step-by-Step Validation
// πΉ Your Core Function
// int CheckBalanced(Node* root)
// β Base case
// if(root==NULL) return 0;
// π Height of empty tree = 0 β
// β Left subtree check
// int leftheight=CheckBalanced(root->left);
// if(leftheight==-1) return -1;
// π If left subtree already unbalanced β stop early
// β This is optimization (important)
// β Right subtree check
// int rightheight=CheckBalanced(root->right);
// if(rightheight==-1) return -1;
// β Same logic
// β Balance condition
// if(abs(leftheight-rightheight)>1) return -1;
// β Correct definition of balanced tree
// β Return height
// return max(leftheight,rightheight)+1;
// β Correct height calculation
// πΉ Wrapper Function
// bool IsBalanced(Node* root){
// return CheckBalanced(root)!=-1;
// }
// β Perfect conversion to boolean
// π Dry Run (Your Tree)
// 1
// / \
// 2 3
// / \
// 4 5
// Heights:
// 4 β 0
// 5 β 0
// 2 β 1
// 3 β 0
// 1 β 2
// Check:
// β£1β0β£=1β€1
// β Balanced β Output = 1
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int CheckBalanced(Node* root){
if(root==NULL) return 0;
int leftheight=CheckBalanced(root->left);
if(leftheight==-1) return -1;
int rightheight=CheckBalanced(root->right);
if(rightheight==-1) return -1;
if(abs(leftheight-rightheight)>1) return -1;
return max(leftheight,rightheight)+1;
}
bool IsBalanced(Node* root){
return CheckBalanced(root)!=-1;
}
int main(){
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);
cout<<IsBalanced(root)<<endl;//output=1 (true)
return 0;
}
// πΉ 6. Check if Tree is Symmetric
// π§ Mathematical Definition
// A tree is symmetric if the left subtree is a mirror reflection of the right subtree.
// Base case:
// A tree is symmetric if the left subtree is a mirror reflection of the right subtree.
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to check if it is symmetric.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Check if Tree is Symmetric
// β
Your Logic (Mathematical View)
// You are using:
// Symmetric(T)=Symmetric(left,right)
// Base case:
// Symmetric(NULL,NULL)=true
// Symmetric(node1,node2)=false if one is NULL and the other is not
// Symmetric(node1,node2)=false if node1.val!=node2.val
// Symmetric(node1,node2)=Symmetric(node1.left,node2.right) and Symmetric(node1.right,node2.left) otherwise
// π Step-by-Step Validation
// πΉ Your Core Function
// bool CheckSymmetric(Node* left,Node* right){
// β Base case 1
// if(left==NULL && right==NULL) return true;
// β Base case 2
// if(left==NULL || right==NULL) return false;
// β Value comparison
// if(left->val!=right->val) return false;
// β Recursive calls
// return CheckSymmetric(left->left,right->right) && CheckSymmetric(left->right,right->left);
// }
// πΉ Wrapper Function
// bool IsSymmetric(Node* root){
// return CheckSymmetric(root,root);
// }
// β Perfect symmetry check by comparing the tree with itself
// π Dry Run (Your Tree)
// 1
// / \
// 2 2
// / \
// 3 4
// CheckSymmetric(1,1) β CheckSymmetric(2,2) && CheckSymmetric(2,2)
// CheckSymmetric(2,2) β CheckSymmetric(3,4) && CheckSymmetric(4,3)
// CheckSymmetric(3,4) β false (values differ)
// β Not symmetric β Output = 0 (false)
// πΉ C++ Example: Check if Tree is Symmetric
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
bool CheckSymmetric(Node* left,Node* right){
if(left==NULL && right==NULL) return true;
if(left==NULL || right==NULL) return false;
if(left->val!=right->val) return false;
return CheckSymmetric(left->left,right->right) && CheckSymmetric(left->right,right->left);
}
bool IsSymmetric(Node*root){
return CheckSymmetric(root,root);
}
int main(){
Node* root=new Node(1);
root->left=new Node(2);
root->right=new Node(2);
root->left->left= new Node(3);
root->left->right= new Node(4);
cout<<IsSymmetric(root)<<endl;//output=0 (false)
}
// πΉ 7. Binary Search (Recursion)
// π§ Mathematical Definition
// Binary Search(low,high):
// mid=low+(high-low)/2
// if arr[mid]==target return mid
// if target<arr[mid] return Binary Search(low,mid-1)
// if target>arr[mid] return Binary Search(mid+1,high)
// Base case:
// if low>high return -1 (target not found in the array)
// if arr[mid]==target return mid (target found at index mid)
// π§ Time Complexity: O(log n) where n is the number of elements in the array, because we are halving the search space at each step.
// π§ Space Complexity: O(log n) due to the recursive call stack. In the worst case (when the target is not found), the depth of the recursion will be log n.
// πΉ C++ Example: Binary Search (Recursion)
// β
Your Logic (Mathematical View)
// You are using:
// BinarySearch(arr,low,high,target):
// mid=low+(high-low)/2
// if arr[mid]==target return mid
// if target<arr[mid] return BinarySearch(arr,low,mid-1,target)
// if target>arr[mid] return BinarySearch(arr,mid+1,high,target)
// Base case:
// if low>high return -1 (target not found in the array)
// if arr[mid]==target return mid (target found at index mid)
// π Step-by-Step Validation
// πΉ Your Core Function
// int BinarySearch(vector<int>& arr,int low,int high,int target){
// β Base case 1
// if(low>high) return -1;
// β Base case 2
// int mid=low+(high-low)/2;
// if(arr[mid]==target) return mid;
// β Recursive calls
// if(target<arr[mid]) return BinarySearch(arr,low,mid-1,target);
// return BinarySearch(arr,mid+1,high,target);
// }
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int BinarySearch(vector<int>&arr,int low,int high,int target){
if(low>high) return -1;
int mid=low+(high-low)/2;
if(arr[mid]==target) return mid;
if(target<arr[mid]) return BinarySearch(arr,low,mid-1,target);
return BinarySearch(arr,mid+1,high,target);
}
int main(){
int n;
cin>>n;
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
int target;
cin>>target;
int result=BinarySearch(arr,0,n-1,target);
if(result!=-1){
cout<<"Element found at index: "<<result<<endl;
} else {
cout<<"Element not found in the array."<<endl;
}
return 0;
}
// πΉ 8. Count Nodes in Binary Tree
// π§ Mathematical Definition
// CountNodes(T)=CountNodes(left)+CountNodes(right)+1
// Base case:
// CountNodes(NULL)=0
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to count it.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Count Nodes in Binary Tree
// β
Your Logic (Mathematical View)
// You are using:
// CountNodes(T)=CountNodes(left)+CountNodes(right)+1
// Base case:
// CountNodes(NULL)=0
// π Step-by-Step Validation
// πΉ Your Core Function
// int CountNodes(Node* root){
// β Base case
// if(root==NULL) return 0;
// β Recursive calls
// return CountNodes(root->left)+CountNodes(root->right)+1;
// }
// πΉ Wrapper Function (if needed)
// Not needed in this case, as the core function already returns the total count of nodes in the tree.
// π Dry Run (Your Tree)
// 1
// / \
// 2 3
// / \
// 4 5
// CountNodes(4) β 1
// CountNodes(5) β 1
// CountNodes(2) β 3
// CountNodes(3) β 1
// CountNodes(1) β 5
// β Total nodes = 5 β Output = 5
// πΉ C++ Example: Count Nodes in Binary Tree
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int CountNodes(Node* root){
if(root==NULL) return 0;
return CountNodes(root->left)+CountNodes(root->right)+1;
}
int main(){
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);
cout<<CountNodes(root)<<endl;//output=5
return 0;
}
// πΉ 9. Count Leaf Nodes in Binary Tree
// π§ Mathematical Definition
// CountLeafNodes(T)=CountLeafNodes(left)+CountLeafNodes(right)
// Base case:
// CountLeafNodes(NULL)=0
// CountLeafNodes(leaf)=1
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to check if it is a leaf and to count it.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Count Leaf Nodes in Binary Tree
// β
Your Logic (Mathematical View)
// You are using:
// CountLeafNodes(T)=CountLeafNodes(left)+CountLeafNodes(right)
// Base case:
// CountLeafNodes(NULL)=0
// CountLeafNodes(leaf)=1
// π Step-by-Step Validation
// πΉ Your Core Function
// int CountLeafNodes(Node* root){
// β Base case 1
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val = data;
left=NULL;
right=NULL;
}
};
int CountLeafNodes(Node* root){
if(root==NULL) return 0;
if(root->left==NULL && root->right==NULL) return 1;
return CountLeafNodes(root->left) + CountLeafNodes(root->right);
}
int main(){
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);
cout<<CountLeafNodes(root)<<endl;//output=3 (leaf nodes: 4, 5, 3)
return 0;
}
// πΉ 10. Count Non-Leaf Nodes in Binary Tree
// π§ Mathematical Definition
// CountNonLeafNodes(T)=CountNonLeafNodes(left)+CountNonLeafNodes(right)+1
// Base case:
// CountNonLeafNodes(NULL)=0
// CountNonLeafNodes(leaf)=0
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to check if it is a non-leaf and to count it.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Count Non-Leaf Nodes in Binary Tree
// β
Your Logic (Mathematical View)
// You are using:
// CountNonLeafNodes(T)=CountNonLeafNodes(left)+CountNonLeafNodes(right)+1
// Base case:
// CountNonLeafNodes(NULL)=0
// CountNonLeafNodes(leaf)=0
// π Step-by-Step Validation
// πΉ Your Core Function
// int CountNonLeafNodes(Node* root){
// β Base case 1
// if(root==NULL) return 0;
// β Base case 2
// if(root->left==NULL && root->right==NULL) return 0;
// β Recursive calls
// return CountNonLeafNodes(root->left)+CountNonLeafNodes(root->right)+1;
// }
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val = data;
left=NULL;
right=NULL;
}
};
int CountNonLeafNodes(Node* root){
if(root==NULL) return 0;
if(root->left==NULL && root->right==NULL) return 0;
return CountNonLeafNodes(root->left)+ CountNonLeafNodes(root->right) +1;
}
int main(){
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);
cout<<CountNonLeafNodes(root)<<endl;//output=2 (non-leaf nodes: 1, 2)
return 0;
}
// πΉ 11. Count Nodes at K-th Level in Binary Tree
// π§ Mathematical Definition
// CountNodesAtK(T,k)=CountNodesAtK(left,k-1)+CountNodesAtK(right,k-1)
// Base case:
// CountNodesAtK(NULL,k)=0
// CountNodesAtK(node,k)=1 if k==0 else 0
// π§ Time Complexity: O(n) where n is the number of nodes in the tree, because we visit each node once to check if it is at the k-th level and to count it.
// π§ Space Complexity: O(h) where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Count Nodes at K-th Level in Binary Tree
// β
Your Logic (Mathematical View)
// You are using:
// CountNodesAtK(T,k)=CountNodesAtK(left,k-1)+CountNodesAtK(right,k-1)
// Base case:
// CountNodesAtK(NULL,k)=0
// CountNodesAtK(node,k)=1 if k==0 else 0
// π Step-by-Step Validation
// πΉ Your Core Function
// int CountNodesAtK(Node* root,int k){
// β Base case 1
// if(root==NULL) return 0;
// β Base case 2
// if(k==0) return 1;
// β Recursive calls
// return CountNodesAtK(root->left,k-1)+CountNodesAtK(root->right,k-1);
// }
// πΉ Wrapper Function (if needed)
// Not needed in this case, as the core function already returns the count of nodes at the k-th level in the tree.
// π Dry Run (Your Tree)
// 1
// / \
// 2 3
// / \
// 4 5
// CountNodesAtK(1,0) β 1
// CountNodesAtK(1,1) β CountNodesAtK(2,0) + CountNodesAtK(3,0) β 1 + 1 = 2
// CountNodesAtK(1,2) β CountNodesAtK(2,1) + CountNodesAtK(3,1) β (CountNodesAtK(4,0) + CountNodesAtK(5,0)) + 0 β (1 + 1) + 0 = 2
// β Nodes at level 0 = 1 (node: 1) β Output = 1
// β Nodes at level 1 = 2 (nodes: 2, 3) β Output = 2
// β Nodes at level 2 = 2 (nodes: 4, 5) β Output = 2
// πΉ C++ Example: Count Nodes at K-th Level in Binary Tree
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int CountNodesAtK(Node* root,int k){
if(root==NULL) return 0;
if(k==0) return 1;
return CountNodesAtK(root->left,k-1) + CountNodesAtK(root->right,k-1);
}
int main(){
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);
cout<<CountNodesAtK(root,0)<<endl;//output=1 (node at level 0: 1)
cout<<CountNodesAtK(root,1)<<endl;//output=2 (nodes at level 1: 2, 3)
cout<<CountNodesAtK(root,2)<<endl;//output=2 (nodes at level 2: 4, 5)
return 0;
}
// πΉ 12. Count Nodes in Complete Binary Tree
// π§ Mathematical Definition
// CountNodes(T)=2^h-1 where h is the height of the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// π§ Time Complexity: O(log^2 n) where n is the number of nodes in the tree, because we calculate the height of the tree in O(log n) time and then use it to calculate the number of nodes.
// π§ Space Complexity: O(log n) due to the recursive call stack when calculating the height of the tree. In the worst case (skewed tree), this can be O(n).
// πΉ C++ Example: Count Nodes in Complete Binary Tree
// β
Your Logic (Mathematical View)
// You are using:
// CountNodes(T)=2^h-1 where h is the height of the tree
// Base case:
// CountNodes(NULL)=0
// CountNodes(leaf)=1
// π Step-by-Step Validation
// πΉ Your Core Function
// int CountNodes(Node* root){
// β Base case 1
// if(root==NULL) return 0;
// β Base case 2
// if(root->left==NULL && root->right==NULL) return 1;
// β Height calculation
// int leftheight=0,rightheight=0;
// Node* left=root->left;
// while(left!=NULL){
// leftheight++;
// left=left->left;
// }
// Node* right=root->right;
// while(right!=NULL){
// rightheight++;
// right=right->right;
// }
// β Check if left and right heights are equal
// if(leftheight==rightheight) return pow(2,leftheight+1)-1;
// β If heights are equal, it's a perfect binary tree β use formula
// β If heights are not equal, it's a complete binary tree β count nodes recursively
// return 1+CountNodes(root->left)+CountNodes(root->right);
// }
// πΉ Wrapper Function (if needed)
// Not needed in this case, as the core function already returns the total count of nodes in the complete binary tree.
// π Dry Run (Your Tree)
// 1
// / \
// 2 3
// / \
// 4 5
// CountNodes(1) β leftheight=2, rightheight=2 β return 2^(2+1)-1=7
// β Total nodes = 5 β Output = 5
// πΉ C++ Example: Count Nodes in Complete Binary Tree
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node* left;
Node* right;
Node(int data){
val=data;
left=NULL;
right=NULL;
}
};
int CountNodes(Node* root){
if(root==NULL) return 0;
if(root->left==NULL && root->right==NULL) return 1;
int leftheight=0,rightheight=0;
Node* left=root->left;
while(left!=NULL){
leftheight++;
left=left->left;
}
Node* right=root->right;
while(right!=NULL){
rightheight++;
right=right->right;
}
if(leftheight==rightheight) return pow(2,leftheight+1)-1;
return 1+CountNodes(root->left) + CountNodes(root->right);
}
int main(){
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);
cout<<CountNodes(root)<<endl;//output=5
return 0;
}
// πΉ 13. Count Nodes in Perfect Binary Tree
// 2. Perfect Binary Tree β
(ONLY CASE WHERE FORMULA WORKS)
// π Example
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
// Levels = 3
// Edges height = 2
// Using formulas:
// π₯ Final Truth (Remember This)
// Height Type Formula