-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentical.java
More file actions
31 lines (25 loc) · 1020 Bytes
/
Identical.java
File metadata and controls
31 lines (25 loc) · 1020 Bytes
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
package BinaryTree;
public class Identical {
public static void main(String args[]) {
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(2);
root1.right = new TreeNode(3);
root1.right.left = new TreeNode(4);
root1.right.right = new TreeNode(5);
TreeNode root2 = new TreeNode(1);
root2.left = new TreeNode(2);
root2.right = new TreeNode(3);
root2.right.left = new TreeNode(4);
if (isIdentical(root1, root2))
System.out.println("Two Trees are identical");
else System.out.println("Two trees are non-identical");
}
public static boolean isIdentical(TreeNode node1 , TreeNode node2)
{
if (node1 == null && node2 == null)
return true;
else if (node1 == null || node2 == null)
return false;
return ((node1.data == node2.data) && isIdentical(node1.left, node2.left) && isIdentical(node1.right, node2.right));
}
}