Mirror Image of a Binary Tree

We take a binary tree and iterate from the root till the lowest sub tree. Each time we visit a node, we swap left and right child. This logic is recursive and execution goes to entire sub-trees level and completes the process. The resulting binary tree a mirror image of the original binary tree.

Mirror a Binary Tree

Source Code

void mirror_tree (struct node* node) { 
  if (node==NULL) { 
  } else { 
    struct node* temp; 
    /* recurse to do the subtrees */
    mirror_tree (node->left); 
    mirror_tree (node->right); 
    /* swap the pointers in this node */
    temp = node->left; 
    node->left = node->right; 
    node->right = temp; 

About our authors: Team EQA

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.