forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdenticalTrees.java
48 lines (42 loc) · 1.3 KB
/
IdenticalTrees.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
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinarySearchTree;
import static java.lang.System.out;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 5:36 PM
*/
public class IdenticalTrees {
/**
* Checks whether two trees having their roots at node1 and node2
* are identical or not.
*
* @param node1
* @param node2
* @param <E>
* @return
*/
public static <E extends Comparable<E>> boolean isIdentical(BinaryNode<E> node1, BinaryNode<E> node2) {
if (node1 == null && node2 == null) return true;
if (node1 == null && node2 != null || (node1 != null && node2 == null)) return false;
if (node1.value == node2.value) {
return true && isIdentical(node1.left, node2.left) && isIdentical(node1.right, node2.right);
} else {
return false;
}
}
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
bst.put(6);
bst.put(3);
bst.put(5);
bst.put(7);
bst.put(8);
bst.put(9);
out.println(IdenticalTrees.isIdentical(bst.root.right, bst.root.right));
out.println(IdenticalTrees.isIdentical(bst.root.right, bst.root));
}
}