二叉树的层次遍历
·
二叉树的基础操作总结
https://blog.csdn.net/hansionz/article/details/81908134
队列的基础操作总结
https://blog.csdn.net/hansionz/article/details/81636644
层次遍历
层次遍历,就是从上到下一层一层的遍历
例如:
思路:
代码实现
void BinaryTreeLevelOrder(BTNode* root)
{
Queue q;
//树为空,直接返回
if (root == NULL)
{
return;
}
QueueInit(&q);
//先将根节点入队
QueuePush(&q, root);
while (QueueEmpty(&q))
{
//出队保存队头并访问
BTNode* front = QueueFront(&q);
printf("%c", front->_data);
QueuePop(&q);
//将出队结点的左子树根入队
if (front->_left)
QueuePush(&q, front->_left);
//将出队结点的右子树根入队
if (front->_right)
QueuePush(&q, front->_right);
}
}
更多推荐
相关推荐
查看更多
DeepSeek-V3-0324

DeepSeek最新推出DeepSeek-V3-0324版本,参数量从6710亿增加到6850亿,在数学推理、代码生成能力以及长上下文理解能力方面直线飙升。
Open-Sora

Open-Sora:为所有人实现高效视频制作
Python

All Algorithms implemented in Python
所有评论(0)