问题
问答题
二叉链表为存储结构,写出二叉树宽度的算法。所谓宽度,是指二叉树的各层上,具有结点数最多的那一层上的结点总数。
答案
参考答案:typedef struct BiTNode
{
TElemType data;
BiTNode*lchild,*rchild; ∥左右孩子指针
}BiTNode,*BiTree;
int width(BiTree*T)
{
BiTree P=T,q [M]:
int front=-1,rear=-1:
int count=0,right;
int max=0:
if(P!=NULL)
{q[++rear]=P;max=1;right=rear;}
while(front!=rear)
{
P=q[++front];
if(T->lchild!=NULL)
{q[+-]-rear]=T->lchild;count++;)
if(T->rchild!=NULL)
{q[++rear]=T->rchild;count++;)
if(front==right)
{
if(max<count)max=count:
count=0:
right=rear;
}
}
return(max);
}