设计程序,按先序创建二叉树的二叉链表;然后层序遍历二叉树。
输入格式:
按先序输入一棵二叉树。二叉树中每个结点的键值用字符表示,字符之间不含空格。注意空树信息也要提供,以#字符表示空树。
输出格式:
输出层序遍历二叉树的序列。序列中不含空格、不含#。
输入样例:
abc##d##e#f##
输出样例:
abecdf
代码示例如下:
#include<bits/stdc++.h>
#define lc tr[i].l
#define rc tr[i].r
#define tc tr[i].ch
using namespace std;
const int N = 1e6+100;
struct node{
char ch;
int l,r;
}tr[N];
int tot;
void build(int i){
tc = getchar();
if(tc=='#') return;
build(lc=++tot);
build(rc=++tot);
}
int main()
{
build(++tot);
queue<int> q;
q.push(1);
while(q.size()){
int i=q.front();q.pop();
if(tc=='#') continue;
putchar(tc);
q.push(lc); q.push(rc);
}
return 0;
}