二叉树的非递归层次遍历算法

以二叉链表存储的二叉树进行层次遍历,可以利用队列来完成。

1.将非空的根节点指针入队列。

2.将对头元素出队列,并访问,再将该结点非空的左右指针入队列。

3.重复2,直到队列为空为止。

void levellist(BitTree *T)
{
    Squeue Q;
    BitTree *p;
    InitQueue(&Q);
    if(T)InQueue(&Q,T);
    while(!EmptyQueue(&Q))
    {
        OutQueue(&Q,&p);
        printf("%c",p->data);
        if(p->lchild)InQueue(&Q,q->lchild);
        if(p->rchild)InQueue(&Q,p->rchild);
    }
}
原文地址:https://www.cnblogs.com/wzqstudy/p/10096655.html