Mirror of a Binary Tree ★
April 23, 2008
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;
}
}
Does this swaps the null nodes (leaf nodes) too ?
Yes, of course.