File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ class Node
3
+ {
4
+ int data ;
5
+ Node left ;
6
+ Node right ;
7
+ Node (int data )
8
+ {
9
+ this .data =data ;
10
+ left =right =null ;
11
+ }
12
+ }
13
+
14
+ class BinaryTree {
15
+ Node root ;
16
+ BinaryTree ()
17
+ {
18
+ root = null ;
19
+ }
20
+ BinaryTree (int data )
21
+ {
22
+ this .root =new Node (data );
23
+ }
24
+
25
+ boolean isIdentical (Node root1 , Node root2 )
26
+ {
27
+ if (root1 ==null && root2 ==null )
28
+ return true ;
29
+ if (root1 ==null || root2 ==null )
30
+ return false ;
31
+ return root1 .data ==root2 .data &&
32
+ isIdentical (root1 .left ,root2 .left ) &&
33
+ isIdentical (root1 .right ,root2 .right );
34
+ }
35
+ }
36
+
37
+ class identicalBtree {
38
+ public static void main (String [] args ) {
39
+
40
+ BinaryTree bt =new BinaryTree (2 );
41
+ bt .root .left =new Node (3 );
42
+ bt .root .right =new Node (5 );
43
+ bt .root .left .right =new Node (9 );
44
+ bt .root .right .left =new Node (7 );
45
+
46
+ BinaryTree bt1 =new BinaryTree (2 );
47
+ bt1 .root .left =new Node (3 );
48
+ bt1 .root .right =new Node (5 );
49
+ bt1 .root .left .right =new Node (9 );
50
+ bt1 .root .right .left =new Node (7 );
51
+
52
+ System .out .println (bt .isIdentical (bt .root ,bt1 .root ));
53
+
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments