Skip to content

Commit 7fcc222

Browse files
committed
check two trees are identical
1 parent e2fbcf9 commit 7fcc222

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Trees/identicalBtree.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

0 commit comments

Comments
 (0)