#include <bits/stdc++.h>//不要忘了改用万能头文件
using namespace std;
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}btnode,*btree;
void buildtree(btree &t)
{
char ch=getchar();
if(ch=='@')
t=NULL;
else
{
t=new btnode;
t->data=ch;
buildtree(t->lchild);
buildtree(t->rchild);
}
}
void leveltraverse(btnode *t)
{
queue<btnode*>q;//学会用这个队列的用法
q.push(t);
while(!q.empty())
{
printf("%c",q.front()->data);
if(q.front()->lchild)q.push(q.front()->lchild);
if(q.front()->rchild)q.push(q.front()->rchild);
q.pop();
}
}
int main()
{
btree t;
buildtree(t);
leveltraverse(t);
printf("\n");
return 0;
}
/*ABD@F@@@CE@@@*/
二叉树的层次遍历
最新推荐文章于 2024-07-23 18:35:33 发布