-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMirrorTheTree.java
More file actions
41 lines (31 loc) · 987 Bytes
/
Copy pathMirrorTheTree.java
File metadata and controls
41 lines (31 loc) · 987 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
32
33
34
35
36
37
38
39
40
41
// Convert a tree to its mirror and check weather if two trees are mirror of each other
public class MirrorTheTree extends BinaryTree{
public static Node mirrorTree(Node root) {
if(root != null) {
mirrorTree(root.left);
mirrorTree(root.right);
Node temp = root.left;
root.left = root.right;
root.right = temp;
}
return root;
}
// Check if two trees are mirror of each other
public static Boolean checkMirror(Node root1, Node root2) {
if(root1 == null && root2 == null)
return true;
if(root2 == null && root1 != null)
return false;
if(root1.data != root2.data)
return false;
else
return (checkMirror(root1.left, root2.right)
&& checkMirror(root1.right, root2.right));
}
public static void main(String[] args) {
BinaryTree.PreOrderIteratively(BinaryTree.BinaryTree3());
System.out.println("----------------");
Node root = mirrorTree(BinaryTree.BinaryTree3());
BinaryTree.PreOrderIteratively(root);
}
}