用数组保存树,按层从左至右存入。假如父节点在数组中的下标为a,则它的左儿子在数组中的下标是2a,右儿子则是2a+1。
判断完整性可以这样来做:数组中的零表示这个下标对应的节点是空的,以一个空的节点为根搜索这个空节点的子树,假如搜索到某个节点是不空的,这个树就是不完整的。因为没有必要重复搜索同一个节点,可以在搜索过程中将搜索过的节点标记成-1,下次碰到就跳过。
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int tree[5000000 ],max;
int check(int r)
{
if(r > max)
return 1 ;
if(tree[r] > 0 )
return 0 ;
else
tree[r] = -1 ;
return check(r * 2) && check(r * 2 + 1 );
}
void solve()
{
int cnt = 0 , i,cmpt = 1 ;
max = 0 ;
memset(tree,0,5000000*sizeof(int ));
while(1 )
{
long a,b;
char tn[256 ];
i = a = 1 ;
if(scanf("%s",tn)== EOF)
exit(0 );
else if(strcmp(tn,"()")==0 )
break ;
if(cmpt == 1 )
{
b = atol(&tn[1 ]);
while(tn[i++] != ',' );
while(tn[i] != ')' )
{
if(tn[i] == 'L' )
a = a * 2 ;
else if(tn[i] == 'R' )
a = a * 2 + 1 ;
i++ ;
}
max = a > max ? a : max;
if(tree[a] == 0 )
tree[a] = b;
else
cmpt = 0 ;
cnt++ ;
}
}/*while*/
if(cmpt == 0 )
{
printf("not complete" );
putchar(10 );
return ;
}
for(i = 1; i <= max / 2; i++ )
{
if(tree[i] == 0 && check(i)== 0 )
{
printf("not complete" );
putchar(10 );
return ;
}
}
i = 1 ;
while(cnt > 0 )
{
if(tree[i] > 0 )
{
if(--cnt != 0 )
printf("%d " ,tree[i]);
else
printf( "%d" ,tree[i]);
}
i++ ;
}
putchar(10 );
}
void main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r" ,stdin);
#endif
while(1 )
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}
#include <stdlib.h>
#include <string.h>
int tree[5000000 ],max;
int check(int r)
{
if(r > max)
return 1 ;
if(tree[r] > 0 )
return 0 ;
else
tree[r] = -1 ;
return check(r * 2) && check(r * 2 + 1 );
}
void solve()
{
int cnt = 0 , i,cmpt = 1 ;
max = 0 ;
memset(tree,0,5000000*sizeof(int ));
while(1 )
{
long a,b;
char tn[256 ];
i = a = 1 ;
if(scanf("%s",tn)== EOF)
exit(0 );
else if(strcmp(tn,"()")==0 )
break ;
if(cmpt == 1 )
{
b = atol(&tn[1 ]);
while(tn[i++] != ',' );
while(tn[i] != ')' )
{
if(tn[i] == 'L' )
a = a * 2 ;
else if(tn[i] == 'R' )
a = a * 2 + 1 ;
i++ ;
}
max = a > max ? a : max;
if(tree[a] == 0 )
tree[a] = b;
else
cmpt = 0 ;
cnt++ ;
}
}/*while*/
if(cmpt == 0 )
{
printf("not complete" );
putchar(10 );
return ;
}
for(i = 1; i <= max / 2; i++ )
{
if(tree[i] == 0 && check(i)== 0 )
{
printf("not complete" );
putchar(10 );
return ;
}
}
i = 1 ;
while(cnt > 0 )
{
if(tree[i] > 0 )
{
if(--cnt != 0 )
printf("%d " ,tree[i]);
else
printf( "%d" ,tree[i]);
}
i++ ;
}
putchar(10 );
}
void main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r" ,stdin);
#endif
while(1 )
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}