今天难得有时间,就整理下上周末做的一道题吧。
这是一道简单的二叉树加前序搜索,只要能看懂题目的意思基本上就问题不大。
#include <stdio.h>
int n,k,ans=0,data[10000];
typedef struct
{
int data,left,right,sum;
}tree;
tree treepoint[10000];
void creattree()
{
int i,number;
for(i=1;i<2*n;i++)
{
treepoint[i].data=i;
treepoint[i].left=2*i;
treepoint[i].right=2*i+1;
}
for(i=1;i<=k;i++)
{
number=data[i];
while(number/2)
{
number/=2;
treepoint[number].sum++;
}
}
}
void search(int point)
{
ans++;
if(treepoint[point].sum<=1)
return ;
search(treepoint[treepoint[point].left].data);
search(treepoint[treepoint[point].right].data);
}
int main()
{
int i,number;
scanf("%d%d",&n,&k);
for(i=1;i<=k;i++)
{
scanf("%d",&number);
data[i]=number+n-1;
}
creattree();
search(1);
printf("%d\n",ans);
return 0;
}