#include <stdio.h>
#include <malloc.h>
#include <string.h>
int k,q,ans[101];
typedef struct treeNode{
int val;
int f;
struct treeNode *left,*right;
}Node;
Node *root;
Node *newnode(int v)
{
Node *u=(Node*)malloc(sizeof(Node));
if(u != NULL)
{
u->val=v;
u->left=u->right=NULL;
}
return u;
}
void addnode(Node *r,Node *u)
{
if(r == NULL)
return;
if(u->val > r->val)
{
addnode(r->right,u);
if(r->right==NULL){
r->right=u;
ans[q++]=r->val;
}
}
else if(u->val < r->val)
{
addnode(r->left,u);
if(r->left==NULL){
r->left=u;
ans[q++]=r->val;
}
}
}
void remove_tree(Node* u) {
if(u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
free(u);
}
int main()
{
//freopen("D:\\in.txt","r",stdin);
int n,v;
while(scanf("%d",&n)==1)
{
q=0;
scanf("%d",&v);
remove_tree(root);
root=newnode(v);
ans[q++]=-1;
for(int i=1;i<n;i++)
{
scanf("%d",&v);
Node *u=newnode(v);
addnode(root,u);
}
for(int j=0;j<n;j++)
{
printf("%d\n",ans[j]);
}
}
}
/**************************************************************
Problem: 1467
User: lhcc1115
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/
九度代码1467
最新推荐文章于 2022-02-25 19:38:17 发布