5
5
import java .util .*;
6
6
7
7
class BT {
8
- class Node {
8
+ static class Node {
9
9
int data ;
10
10
Node left ;
11
11
Node right ;
@@ -17,7 +17,7 @@ class Node{
17
17
}
18
18
}
19
19
20
- Node root ;
20
+ Node root = null ;
21
21
22
22
void insert (int data ){
23
23
root =insertRec (root ,data );
@@ -464,6 +464,87 @@ private void spiralOrderfn(Node root){
464
464
System .out .println ();
465
465
}
466
466
}
467
+ public void printBetweenTwoLevels (int min ,int max ) {
468
+ printBetweenTwoLevelsfn (root ,min ,max );
469
+ }
470
+ private void printBetweenTwoLevelsfn (Node root ,int min ,int max ) {
471
+ if (root == null )
472
+ return ;
473
+ Queue <Node > q = new LinkedList <>();
474
+ q .add (root );
475
+ int level =1 ;
476
+ while (true ) {
477
+ int count =q .size ();
478
+ if (count ==0 || level >max )
479
+ break ;
480
+ while (count >0 ) {
481
+ Node node = q .remove ();
482
+ if (level >=min && level <=max )
483
+ System .out .print (node .data +" " );
484
+ if (node .left != null )
485
+ q .add (node .left );
486
+ if (node .right != null )
487
+ q .add (node .right );
488
+ count --;
489
+ }
490
+ level ++;
491
+ System .out .println ();
492
+ }
493
+ }
494
+ public void maxWidth () {
495
+ maxWidthfn (root );
496
+ }
497
+ private void maxWidthfn (Node root ) {
498
+ if (root == null )
499
+ return ;
500
+ Queue <Node > q = new LinkedList <>();
501
+ q .add (root );
502
+ int maxwidth =0 ;
503
+ while (true ) {
504
+ int count =q .size ();
505
+ if (count >maxwidth )
506
+ maxwidth =count ;
507
+ if (count ==0 )
508
+ break ;
509
+ while (count >0 ) {
510
+ Node node = q .remove ();
511
+ if (node .left != null )
512
+ q .add (node .left );
513
+ if (node .right != null )
514
+ q .add (node .right );
515
+ count --;
516
+ }
517
+ }
518
+ System .out .println (maxwidth );
519
+ }
520
+ public boolean ifMirrorTree (Node root1 ,Node root2 ){
521
+ if (root1 ==null && root2 ==null )
522
+ return true ;
523
+ if (root1 ==null ||root2 ==null )
524
+ return false ;
525
+ return root1 .data ==root2 .data && ifMirrorTree (root1 .left ,root2 .right )&&ifMirrorTree (root1 .right ,root2 .left );
526
+ }
527
+ public boolean ifMirrorStructureTree (Node root1 ,Node root2 ){
528
+ if (root1 ==null && root2 ==null )
529
+ return true ;
530
+ if (root1 ==null ||root2 ==null )
531
+ return false ;
532
+ return ifMirrorStructureTree (root1 .left ,root2 .right )&&ifMirrorStructureTree (root1 .right ,root2 .left );
533
+ }
534
+ public boolean ifSameStructureTree (Node root1 ,Node root2 ){
535
+ if (root1 ==null && root2 ==null )
536
+ return true ;
537
+ if (root1 ==null ||root2 ==null )
538
+ return false ;
539
+ return ifMirrorStructureTree (root1 .left ,root2 .left )&&ifMirrorStructureTree (root1 .right ,root2 .right );
540
+ }
541
+ public boolean isFoldable (){
542
+ Node node =root ;
543
+ if (node ==null )
544
+ return true ;
545
+ else
546
+ return ifMirrorStructureTree (node .left ,node .right );
547
+ }
467
548
}
468
549
469
550
0 commit comments