核心思想:首先根节点入队,若队列非空则做循环,若根节点有左右孩子,则左右孩子入队,第一个节点出队,循环直到队列为空。
#ifndef _BTREE_H_
#define _BTREE_H_
typedef char BTDataType;
typedef struct BinaryTreeNode{
BTDataType data;
struct BinaryTreeNode* lchild;
struct BinaryTreeNode* rchild;
}BTNode;
void BinaryTreeDestory(BTNode* root);
void BinaryTreeLevelOrder(BTNode* root);
#endif/*_BTREE_H_*/
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "BTree.h"
typedef BTNode * QuDataType;
typedef struct QueueNode
{
QuDataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue {
QueueNode * _head;
QueueNode * _rear;
}Queue;
void QueueInit(Queue* plist);
void QueueDestory(Queue* plist);
void QueuePop(Queue* plist);
void QueuePush(Queue* plist, QuDataType x);
QuDataType QueueTop(Queue* plist);
int QueueIsEmpty(Queue* plist);
#endif //_QUEUE_H_
#include "queue.h"
#include <std

本文介绍了一种使用队列实现二叉树层次遍历的方法。通过将根节点入队,然后在队列非空的情况下,循环处理节点,将其左右孩子依次入队并出队当前节点,直至队列为空,实现层次遍历。
最低0.47元/天 解锁文章
6816

被折叠的 条评论
为什么被折叠?



