Skip to content

Instantly share code, notes, and snippets.

@thmain
Created February 23, 2016 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/2770746b16ff2e81da14 to your computer and use it in GitHub Desktop.
Save thmain/2770746b16ff2e81da14 to your computer and use it in GitHub Desktop.
public class MirrorTree {
public void mirror(Node root){
print(root);
Node x = mirrorTree(root);
System.out.print("\n Mirror Image ");
print(x);
}
public Node mirrorTree(Node root){
if(root!=null){
Node t = root.left;
root.left = root.right;
root.right = t;
mirrorTree(root.right);
mirrorTree(root.left);
}
return root;
}
public void print(Node root){
if(root!=null){
print(root.left);
System.out.print("" + root.data);
print(root.right);
}
}
public static void main (String[] args) throws java.lang.Exception
{
Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(6);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(7);
MirrorTree i = new MirrorTree();
i.mirror(root);
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right =null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment