struct bitree
{
int data;
struct bitree *lchild, *rchild;
};
void deep_traverse(bitree *t)
{
queue<struct bitree*> q; //给定一个队列
q.push(t); //先将头结点入队
while (!q.empty()) { // 如果队列不空
struct bitree *p = q.top(); //取出对头元素
q.pop();
printf("%d ", p->data);
if (p->lchild) q.push(p->lchild); //如果有左孩子,则左孩子入队
if (p->rchild) q.push(p->rchild); //如果有右孩子,则右孩子入队
}
}
二叉树层次遍历
最新推荐文章于 2023-07-05 21:27:44 发布