xAbhishek.com

Mirror of a Binary Tree

Creating a mirror of a Binary Tree(or Binary Search Tree)

void BinaryTree::mirror(tree_node* node) { 

  if (node==NULL) {
    return;
  }
  else {
    tree_node* temp;

    // do the subtrees
    mirror(node->left);
    mirror(node->right);

    // swap the pointers in this node
    temp = node->left;
    node->left = node->right;
    node->right = temp;
  }
} 

 

Ping "Mirror of a Binary Tree": http://xabhishek.com/2008/04/23/mirror-of-a-binary-tree/trackback/

2 comments here

  • Does this swaps the null nodes (leaf nodes) too ?

    - ksp #
  • Yes, of course.

Write comment








Copyright 2008 Abhishek Nandakumar I Google, Therefore I Am