-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIdenticalBinaryTree.java
64 lines (46 loc) · 1.49 KB
/
IdenticalBinaryTree.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class IdenticalBinaryTree {
class Node {
int data;
Node left;
Node right;
}
public Node createNewNode(int data) {
Node node = new Node();
node.data = data;
node.left = null;
node.left = null;
return node;
}
public boolean isIdentical(Node root1, Node root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 == null || root2 == null) {
return false;
}
return root1.data == root2.data && isIdentical(root1.left, root2.left) && isIdentical(root1.right, root2.right);
}
public static void main(String[] args) {
IdenticalBinaryTree opr = new IdenticalBinaryTree();
Node root1 = opr.createNewNode(8);
root1.left = opr.createNewNode(3);
root1.right = opr.createNewNode(10);
root1.left.left = opr.createNewNode(1);
root1.left.right = opr.createNewNode(6);
root1.left.right.left = opr.createNewNode(4);
root1.left.right.right = opr.createNewNode(7);
root1.right.right = opr.createNewNode(14);
root1.right.right.left = opr.createNewNode(13);
Node root2 = opr.createNewNode(8);
root2.left = opr.createNewNode(3);
root2.right = opr.createNewNode(10);
root2.left.left = opr.createNewNode(1);
root2.left.right = opr.createNewNode(6);
root2.left.right.left = opr.createNewNode(4);
root2.left.right.right = opr.createNewNode(7);
root2.right.right = opr.createNewNode(14);
root2.right.right.left = opr.createNewNode(13);
boolean isIdentical = opr.isIdentical(root1, root2);
System.out.println(isIdentical);
}
}