文章
60
粉丝
361
获赞
43
访问
524.6k
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
typedef struct node
{
int data;
struct node *lchild,*rchild;
}*tree;
void paixu(tree &T,int temp,int parent)
{
if(T==NULL)
{
T=new node;
T->data=temp;
T->lchild =NULL;
T->rchild=NULL;
cout<<parent<<endl;
return;
}
else
{
if(temp==T->data)
return;
else if(temp<T->data)
{
parent=T->data ;
paixu(T->lchild ,temp,parent);
}
else if(temp>T->data)
{
parent=T->data ;
paixu(T->rchild ,temp,parent);
}
}
return;
}
int main()
{
int n;
while(cin>>n)
{
int temp;
tree T=NULL;
int x=-1;
for(int i=0;i<n;i++)
{
cin>>temp;
paixu(T,temp,x);
}
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发