problem 1167

Trees on the Level

      用数组保存树,按层从左至右存入。假如父节点在数组中的下标为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

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值