Write a code to construct a Mirror of Binary Tree as below.
Solution 1 ) Create a New Tree and store nodes in the way we want (mirrored way)
Algo :
struct node{
int value;
struct node* left;
struct node* right;
};
struct node *mirrorTree(struct node *root){
struct node *temp;
if(root==NULL)
return(NULL);
temp = (struct node *) malloc(sizeof(struct node));
temp->value = root->value;
temp->left = mirrorTree(root->right);
temp->right = mirrorTree(root->left);
return(temp);
}
}
See Video :
Solution 1 ) Create a New Tree and store nodes in the way we want (mirrored way)
Algo :
struct node{
int value;
struct node* left;
struct node* right;
};
struct node *mirrorTree(struct node *root){
struct node *temp;
if(root==NULL)
return(NULL);
temp = (struct node *) malloc(sizeof(struct node));
temp->value = root->value;
temp->left = mirrorTree(root->right);
temp->right = mirrorTree(root->left);
return(temp);
}
Solution 2 ) In place change ( Use Recursion )
void mirror(struct node* node)
{
if (node==NULL)
return;
else
{
struct node* temp;
mirror(node->left);
mirror(node->right);
/* swap the pointers */
temp = node->left;
node->left = node->right;
node->right = temp;
}
See Video :