填空题
[说明]
求树的宽度,所谓宽度是指在二叉树的各层上,具有结点数最多的那一层的结点总数。本算法是按层次遍历二叉树,采用一个队列q,让根结点入队列,若有左右子树,则左右子树根结点入队列,如此反复,直到队列为空。
[函数]
int Width ( BinTree *T
{
int front=-1, rear=-1; /*队列初始化*/
int flag=0, count=0, p; /*p用于指向树中层的最右边的结点, flag 记录层中结点数的最大值*/
if ( T!=Null)
{
rear++;
{{U}} (1) {{/U}};
flag=1;
p=rear;
}
while ({{U}} (2) {{/U}})
{
front++;
T=q [front]];
if (T->lchild!=Null )
{
roar+-+;
{{U}} (3) {{/U}};
count++;
}
if ( T->rchild!=Null )
{
rear++; q[rear]=T->rchild;
{{U}} (4) {{/U}};
}
if (front==p ) // 当前层已遍历完毕
{
if({{U}} (5) {{/U}})
flag=count;
count=0;
p=rear, //p 指向下一层最右边的结点
}
}
return ( flag );
}
【正确答案】
1、(1) q [rear]=T (2) front<p
【答案解析】(3) q [rear]=T->lchild (4) count++
(5) flag<count