PRoblem Link:http://139.129.36.234/problem.php?id=1274
52 5 1 3 4样例输出
-1 2 2 5 3提示
来源
北邮机试真题
AC code:
#include<iostream>#include<algorithm>#include<stdio.h>#include<map>#include<math.h>#include<string.h>#include<queue>#include<vector>#include<set>#define LL long long#define exp 1e-9#define MAXN 1000010using namespace std;typedef struct BTNode{ int data; BTNode *lchild; BTNode *rchild;}BTNode;void insertBT(BTNode *&bt,int x){ BTNode *pre,*p; p=bt; int dir=0; if(p==NULL) { bt = (BTNode *)malloc(sizeof(BTNode)); bt->data=x; bt->lchild=NULL; bt->rchild=NULL; printf("-1 "); } else { while(p!=NULL) { pre=p; if(p->data>x) { p=p->lchild; } else { p=p->rchild; } } p = (BTNode *)malloc(sizeof(BTNode)); p->data=x; p->lchild=NULL; p->rchild=NULL; if(pre->data>x) { pre->lchild=p; } else { pre->rchild=p; } printf("%d ",pre->data); }}int main(){// freopen("D://in.txt","r",stdin); int n,i,x; scanf("%d",&n); BTNode *bt=NULL; for(i=1;i<=n;i++) { scanf("%d",&x); insertBT(bt,x); } puts(""); return 0;}
新闻热点
疑难解答