输出利用先序遍历创建的二叉树的层次遍历序列
输入
输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树)。
输出
每个用例用一行出该用例对应的二叉树的层次遍历序列。
样例输入
A##
ABC####
AB##C##
ABCD###EF##G##H##
A##B##
样例输出
A
ABC
ABC
ABHCEDFG
A
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int N=105;
int cnt;
struct tree
{
char dat;
tree *lc,*rc;
tree()
{
lc=NULL,rc=NULL;
}
}*root;
void build(tree *&root)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
{
root=NULL;
return;
}
root=new tree();
root->dat=ch;
build(root->lc);
build(root->rc);
}
queue<tree *>q;
void level()
{
if(root==NULL)
return;
q.push(root);
while(!q.empty())
{
tree *cur=q.front();
q.pop();
printf("%c",cur->dat);
if(cur->lc!=NULL)
q.push(cur->lc);
if(cur->rc!=NULL)
q.push(cur->rc);
}
}
int main()
{
build(root);
level();
}
因篇幅问题不能全部显示,请点此查看更多更全内容
怀疑对方AI换脸可以让对方摁鼻子 真人摁下去鼻子会变形
女子野生动物园下车狼悄悄靠近 后车司机按喇叭提醒
睡前玩8分钟手机身体兴奋1小时 还可能让你“变丑”
惊蛰为啥吃梨?倒春寒来不来就看惊蛰
男子高速犯困开智能驾驶出事故 60万刚买的奔驰严重损毁