测试例子:5
1 6 5 9 8
输出例子:1 6 5 9 8
5 8 9 6 1
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node {
Node *rchild;
Node *lchild;
int data ;
};
void preOrder(Node *bt){
printf("%d",bt->data);
if(bt->lchild!=NULL)
preOrder(bt->lchild);
if(bt->rchild!=NULL)
preOrder(bt->rchild);
}
void inOrder(Node *bt){
//printf("%d",bt->data);
if(bt->lchild!=NULL)
inOrder(bt->lchild);
if(bt->rchild!=NULL)
inOrder(bt->rchild);
printf("%d",bt->data);
}
Node *insert(Node *bt,int x)
{
if(bt==NULL)
{
bt=new Node;
bt->data=x;
bt->rchild=bt->lchild=NULL;
return bt;
}else if(x<bt->data)
bt->lchild=insert(bt->lchild,x);
else if(x>bt->data)
bt->rchild=insert(bt->rchild,x);
return bt;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
Node *bt=NULL;
for(int i =0;i<n;i++){
int x;
scanf("%d",&x);
bt=insert(bt,x);
}
preOrder(bt);
printf("\n");
inOrder(bt);
}
system("pause");
return 0;
}