ps:第一个是深度优先遍历,第二个是广度优先遍历。
//思路:用队列实现, 上一层带下一层 ,
先1进;1出带2,4进;2出带3进;4出带5,6进;
3出带7进;5出,5下一层为空,不能带;
6出,6下一层为空,不能带;
7出,7下一层为空,不能带;
//要将队列的项目的Queue.c和Test.c copy到Test的树中
void LevelOrder(BTNode* root)
{
Queue q; //队列里存是节点(结构体)不方便
//改存指针
QueueInit(&q);
if(root)//先把首根放进去
{
QueuePush(&q,root);
}
while(QueueEmpty(&q))
{
BTNode* front=QueueFront(&q);
printf("%d",front->data);
QueuePop(&q);
if(front->left)
{
QueuePush(&q,front->left);
}
if(front->right)
{
QueuePush(&q,front->right);
}
}
printf("\n");
QueueDestroy(&q);
}