How to get Mirror Image of a Binary Search Tree?


What do you mean by Mirror image of a Binary tree? It means, another binary search tree that is a mirror image of the given one.

This is a recursive solution for this problem, just swap the left and right subtrees for each node recursively. Let me know if you have problem understanding it.

struct tnode *mirror(struct tnode *p){
if(p == NULL)
return p ;
else{
mirror(p->left);
mirror(p->right);
struct tnode *temp;
temp=p->left;
p->left=p->right;
p->right=temp;
}
return p;
}

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: