forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdenticalBinaryTree.java
90 lines (71 loc) · 2.47 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package trees;
public class IdenticalBinaryTree {
/*
* Given roots of two binary trees, determine if these trees are identical or not.
* Identical trees have the same layout and data at each node.
*
* Runtime Complexity:
* Linear, O(n).
*
* Memory Complexity:
* O(h)
*
* The recursive solution has O(h) memory complexity as it will consume memory on the stack up to the height
* of binary tree h. It will be O(log n) for a balanced tree and in the worst case can be O(n).
*
* Two trees 'A' and 'B' are identical if:
* data on their roots is same or both roots are null
* left sub-tree of 'A' is identical to the left sub-tree of 'B'
* right sub-tree of 'A' is identical to the right sub-tree of 'B'
* To solve this problem do a depth first traversal on both trees simultaneously and keep comparing the data
* at each level.
*
* */
private Node root1, root2;
private static class Node {
private int data;
private Node left, right;
Node(int item) {
data = item;
left = right = null;
}
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
}
protected boolean isIdentical(Node root1,
Node root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 != null && root2 != null) {
return ((root1.data == root2.data) &&
isIdentical(root1.left, root2.left) &&
isIdentical(root1.right, root2.right));
}
return false;
}
public static void main(String[] args) {
IdenticalBinaryTree tree = new IdenticalBinaryTree();
tree.root1 = new Node(1);
tree.root1.left = new Node(2);
tree.root1.right = new Node(3);
tree.root1.left.left = new Node(4);
tree.root1.left.right = new Node(5);
tree.root2 = new Node(1);
tree.root2.left = new Node(2);
tree.root2.right = new Node(3);
tree.root2.left.left = new Node(4);
tree.root2.left.right = new Node(5);
if (tree.isIdentical(tree.root1, tree.root2))
System.out.println("Both trees are identical");
else
System.out.println("Trees are not identical");
}
}