中序输出其实就是从小到大输出,sort就可以了
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef struct node
{
int data;
struct node *l;
struct node *r;
}Tree;
Tree *creat(int key, Tree *root)
{
if(root==NULL)
{
root=new Tree;
root->l=NULL;
root->r=NULL;
root->data=key;
}
else
{
if(key<root->data)
root->l=creat(key, root->l);
else
root->r=creat(key, root->r);
}
return root;
}
int x;
void intree(Tree *root)
{
if(root)
{
intree(root->l);
if(x==1)
cout<<root->data;
else
cout<<" "<<root->data;
x++;
intree(root->r);
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
Tree *root;
while(cin>>n)
{
root=NULL;
for(int i=0;i<n;i++)
{
int key;
cin>>key;
root=creat(key, root);
}
x=1;
intree(root);
cout<<endl;
}
return 0;
}