<题目>给定一棵二叉树,实现其层次遍历。
方法:利用队列
#include<iostream>
#include<queue>
using namespace std;
typedef struct BinTreeNode
{
int data;
struct BinTreeNode* leftchild;
struct BinTreeNode* rightchild;
}BinTreeNode;
void LevelOrder(BinTreeNode* t)
{
if (t != NULL)
{
queue<BinTreeNode*> q;
q.push(t);
while (!q.empty())
{
BinTreeNode *p = q.front();
q.pop();
printf("%c ", p->data);
if (p->leftchild != NULL)
q.push(p->leftchild);
if (p->rightchild != NULL)
q.push(p->rightchild);
}
}
}