Write an Function to Convert a Binary Tree into its Mirror Tree | Atrenta written Question

Mirror of a Binary Tree T1 is another Binary Tree T2 with left and right children of all non-leaf nodes interchanged.

Example –

       4                                
      / \
     2   5          ---> 
    / \
   1   3
 
       4
      / \
     5   2
        / \
       3   1
      

Algorithm – Mirror(tree):

1.  Call Mirror for left-subtree    i.e., Mirror(left-subtree)
2.  Call Mirror for right-subtree  i.e., Mirror(left-subtree)
3.  Swap left and right subtrees.
          temp = left-subtree
          left-subtree = right-subtree
          right-subtree = temp

Code :

void mirror(struct node* node)
{
  if (node==NULL)
    return;
  else
  {
    struct node* temp;
 
    mirror(node->left);
    mirror(node->right);
 
    /* swap the pointers in this node */
    temp        = node->left;
    node->left  = node->right;
    node->right = temp;
  }
} 

Leave a Reply

Your email address will not be published. Required fields are marked *

For Inserting code :
Paste your code in the comment form, select it and then click the language link

C | C++ | Java |

*