-
Notifications
You must be signed in to change notification settings - Fork 2
/
TwoTreeMirrorSkeleton.java
52 lines (35 loc) · 1.13 KB
/
TwoTreeMirrorSkeleton.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
public class TwoTreeMirrorSkeleton {
class Node {
int data;
Node left;
Node right;
}
public Node createNewNode(int data) {
Node node = new Node();
node.left = null;
node.right = null;
return node;
}
public boolean isMirrorSkeleton(Node root1, Node root2) {
if (root1 == null && root2 == null) {
return true;
}
if ((root1 == null && root2 != null) || (root1 != null && root2 == null)) {
return false;
}
return isMirrorSkeleton(root1.left, root2.right) && isMirrorSkeleton(root1.right, root2.left);
}
public static void main(String[] args) {
TwoTreeMirrorSkeleton tree = new TwoTreeMirrorSkeleton();
Node root1 = tree.createNewNode(2);
root1.left = tree.createNewNode(7);
root1.right = tree.createNewNode(8);
root1.left.left = tree.createNewNode(5);
Node root2 = tree.createNewNode(3);
root2.left = tree.createNewNode(6);
root2.right = tree.createNewNode(7);
root2.right.right = tree.createNewNode(5);
// root2.right.right.left = tree.createNewNode(5);
System.out.println(tree.isMirrorSkeleton(root1, root2));
}
}