You are on page 1of 2

#include<iostream>

#include<queue>
using namespace std;
struct Node
{
int key;
struct Node *left,*right;
};
struct Node *root=NULL;
void mirror(struct Node*ro)
{
if(ro==NULL)
return;
else
{
struct Node*t;
mirror(ro->left);
mirror(ro->right);
t=ro->left;
ro->left=ro->right;
ro->right=t;
}
}
void preOrder(struct Node*root)
{
if(root==NULL)
return;
cout<<root->key<<" ";
preOrder(root->left);
preOrder(root->right);
}
struct Node* insert(struct Node *root,int x)
{
struct Node* p=(struct Node*)malloc(sizeof(struct Node));
p->key=x;
p->right=NULL;
p->left=NULL;
if(root==NULL)
root=p;
else
{
struct Node*pr=NULL;
struct Node*cu=root;
while(cu!=NULL)
{
pr=cu;
if(x<cu->key)
cu=cu->left;
else if(x>cu->key)
cu=cu->right;
}
if(x<pr->key)
pr->left=p;
else if(x>pr->key)
pr->right=p;
}
return root;
}
int main()
{
int n,x,l;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
root=insert(root,x);
}
preOrder(root);
cout<<endl;
mirror(root);
preOrder(root);
}

You might also like