Monday 23 September 2013

Create a mirror of a binary tree

Example
Input
Output

Implementation

public static Tree mirror(Tree root){
if(root == null)
return null;
Tree leftTree = null, rightTree = null;
if(root.left != null)
leftTree = mirror(root.left);
 
     if(root.right != null)
rightTree = mirror(root.right);
root.left = rightTree;
root.right = leftTree;
return root;
}

1 comment:

  1. This is not mirroring right.The connections cannot be changed. For example B connected to D,E cannot be suddenly connected to D,F....

    ReplyDelete