Skip to content

Instantly share code, notes, and snippets.

@vinay13
Created February 16, 2014 00:09
Show Gist options
  • Save vinay13/9027238 to your computer and use it in GitHub Desktop.
Save vinay13/9027238 to your computer and use it in GitHub Desktop.
mirror image of binary tree
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newnode(int data)
{
struct node* node=(struct node*)
malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return(node);
}
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;
}
}
//inorder traversal of tree
void inorder(struct node* node)
{
if(node==NULL)
return;
inorder(node->left);
printf(" %d ",node->data);
inorder(node->right);
}
int main()
{
struct node *root=newnode(1);
root->left=newnode(2);
root->right=newnode(3);
root->left->left=newnode(4);
root->left->right=newnode(5);
printf("before: ");
inorder(root);
mirror(root);
printf("\n After");
inorder(root);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment