思路:
1.先将根节点入队;
2.出队,并将该出队节点的左右子节点入队;
3.循环进行第二步,直到队列空;
/* 二叉树的层次遍历(广度优先) */
#include <stdio.h>
#include <stdlib.h>
typedef struct treeNode *BinTree;
typedef struct listNode *List;
struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;
};
struct listNode
{
BinTree element;
struct listNode *next;
};
/* 链表实现队列 */
List createList(){
List list = (List)malloc(sizeof(struct listNode));
list->element = NULL;
list->next = NULL;
return list;
}
/* 入队 */
void inList(List *tail, BinTree myElement){
if (myElement