Skip to content

Commit a9263a7

Browse files
Add files via upload
1 parent 85abac8 commit a9263a7

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

Trees/BinaryTree/BT.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.*;
66

77
class BT {
8-
class Node{
8+
static class Node{
99
int data;
1010
Node left;
1111
Node right;
@@ -17,7 +17,7 @@ class Node{
1717
}
1818
}
1919

20-
Node root;
20+
Node root=null;
2121

2222
void insert(int data){
2323
root=insertRec(root,data);
@@ -464,6 +464,87 @@ private void spiralOrderfn(Node root){
464464
System.out.println();
465465
}
466466
}
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+
}
467548
}
468549

469550

0 commit comments

Comments
 (0)