/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Node {
public Node left;
public int data;
public Node right;
public Node(int val) {
this.data=val;
}
}
/* Name of the class has to be "Main" only if the class is public. */
class MirrorTree {
public void mirror(Node root) {
if(root== null)
return ;
else
{
mirror(root.left);
mirror(root.right);
swap(root);
}
}
// Swap Sub Trees of a Node
private void swap(Node node) {
Node temp=node.right;
node.right=node.left;
node.left=temp;
}
// inorder display subroutine
public void inorder(Node r) {
if (r != null) {
inorder(r.left);
System.
out.
print(r.
data + "\t"); inorder(r.right);
}
}
public static void main
(String[] args
) { Node root=new Node(12);
Node n1=new Node(14);
Node n2=new Node(18);
Node n3=new Node(10);
root.left=new Node(8);
root.right=new Node(16);
root.left.left=new Node(6);
root.left.right=n3;
root.right.left=n1;
root.right.right=n2;
MirrorTree m=new MirrorTree();
System.
out.
println("Before:"); m.inorder(root);
m.mirror(root);
System.
out.
println("\n After:"); m.inorder(root);
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBOb2RlIHsKCXB1YmxpYyBOb2RlIGxlZnQ7CglwdWJsaWMgaW50IGRhdGE7CglwdWJsaWMgTm9kZSByaWdodDsKCglwdWJsaWMgTm9kZShpbnQgdmFsKSAJewoJCXRoaXMuZGF0YT12YWw7Cgl9Cn0KCgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KIGNsYXNzIE1pcnJvclRyZWUgewoKCXB1YmxpYyB2b2lkIG1pcnJvcihOb2RlIHJvb3QpIHsKCQlpZihyb290PT0gbnVsbCkKCQkJcmV0dXJuIDsKCQllbHNlCgkJewoJCQltaXJyb3Iocm9vdC5sZWZ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