数据结构学习笔记(8)---二叉树的层次遍历

来源:互联网 发布:人工智能 库布里克 编辑:程序博客网 时间:2024/05/20 16:37
二叉树的层序遍历

思路:使用队列 ,首先将根节点进队,然后出队并输出,把该节点的左右子树分别入队,直到队列为空时循环结束

void LayerOrder(PBiTree root){    queue<PBiTree> q;    PBiTree  p;    if (root == NULL)    {        return;    }    q.push(root);    while (!q.empty())    {        p = q.front();        q.pop();        cout << p->date;        if (p->lchild)        {            q.push(p->lchild);        }        if (p->rchild)        {            q.push(p->rchild);        }    }}
阅读全文
0 0