#include<malloc.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
int main()
{
char tree[10000];//储存输入的层序遍历
char s[10][10000];//s字符数组 s[储存第几层][储存每层的节点数据]
gets(tree);
int t=0;
int len=strlen(tree);//输入的长度
int cengshu=log(len)/log(2)+1;//计算有多少层
for(int i=1;i<=cengshu;i++)
for(int j=0;j<pow(2,i-1);j++)//每层最多有2的i-1次方个结点
s[i][j]=tree[t++];//储存到s数组
for(int i=1;i<=cengshu;i++)
{
int geshu=pow(2,i-1);//每层的个数(包含空结点)
if(i%2==0) reverse(s[i],s[i]+geshu);//翻转偶数层数
}
for(int i=1;i<=cengshu;i++)
{
for(int j=0;j<pow(2,i-1);j++)
{
if(s[i][j]!='#') printf("%c ",s[i][j]);//输出结点不为‘#’且不为空的
}
printf("\n");
}
}
18959待修改
最新推荐文章于 2022-04-06 08:24:38 发布