UVA-11234
题意:给出一颗满二叉树的后序遍历,求它从下到上的逐层遍历。
解题思路:利用栈先把二叉树给求出来,在通过队列求出从上到下顺序。反过来输出。
/*************************************************************************
> File Name: UVA-11234.cpp
> Author: Narsh
>
> Created Time: 2016年07月16日 星期六 14时41分08秒
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct node {
int lef,rig;
}a[30000];
int t,n,b[30000],lb,l;
string s;
int main () {
scanf("%d",&t);
while (t--) {
cin>>s;
l=s.length();
s=" "+s;
lb=0;
for (int i = 1; i <= l; i++) {
if ('a' <= s[i] && s[i] <= 'z') {
lb++;
b[lb] = i;
a[i].lef =a[i].rig =-1;
}
if ('A' <= s[i] && s[i] <= 'Z') {
a[i].rig = b[lb];
lb--;
a[i].lef = b[lb];
b[lb] = i;
}
}
lb=1;
b[1]=l;
//printf("\n%c\n",s[b[1]]);
for (int i = 1; i <= lb; i++){
if (a[b[i]].lef != -1) b[++lb] = a[b[i]].lef;
if (a[b[i]].rig != -1) b[++lb] = a[b[i]].rig;
}
for (int i = lb; i >= 1; i--)
printf("%c",s[b[i]]);
printf("\n");
}
}