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) { 
    return; 
  } else { 
    struct node* temp; 
    /* recurse to do the subtrees */
    mirror_tree (node->left); 
    mirror_tree (node->right); 
    /* swap the pointers in this node */
10      temp = node->left; 
11      node->left = node->right; 
12      node->right = temp; 
13    } 
14  }

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

 Vote 0

Similar topics related to this section

Tree and Binary tree, Binary tree, traverse Binary tree, Binary search tree, double Binary tree, mirror Binary tree, height of Binary tree, heap, Complexity,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan