Wednesday, November 16, 2011

Mirroring a Tree Iterative n Recursive


public static void iterative_mirror(BinaryTree root){
if(root==null) return;
   Queue<BinaryTree> q= new LinkedList<BinaryTree>();
   q.add(root);
   BinaryTree curr,temp;
   while(!q.isEmpty()){
curr=q.poll();
temp=curr.left;
curr.left=curr.right;
curr.right=temp;
if(curr.left!=null)
q.add(curr.left);
if(curr.right!=null)
q.add(curr.right);
   }
   LevelOrder(root);
}

public static void rec_mirror(BinaryTree root){
BinaryTree temp;
   if(root==null) return;
   else {
 
  rec_mirror(root.left);
  rec_mirror(root.right);
  temp = root.left;
  root.left=root.right;
  root.right=temp;
   }
   
}

No comments:

Post a Comment