/*
首先建一个表达式树(二叉树),栈算法的表达式对应二叉树的后序遍历,
根据例子发现队列算法的表达式对应二叉树的层序遍历序列的逆序列。
*/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 10004;
char v[MAXN];
int l[MAXN], r[MAXN], n;
int root;
int st[MAXN], f;
int q[MAXN];
char buff[MAXN];
void bfs()
{
int len = n-2;
buff[n-1] = 0;
int fr=0, ed=0;
q[ed++] = root;
while(fr < ed) {
int u = q[fr++];
buff[len--] = v[u];
if(l[u]) q[ed++] = l[u];
if(r[u]) q[ed++] = r[u];
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int T;
scanf("%d\n", &T);
char c;
int a, b;
while(T--) {
n = 1;
f = 0;
while((c = getchar()) != '\n') {
v[n] = c;
l[n] = r[n] = 0;
if(isupper(c)) {
a = st[--f];
b = st[--f];
l[n] = b;
r[n] = a;
}
st[f++] = n++;
}
root = n-1;
bfs();
printf("%s\n", buff);
}
return 0;
}
05-03
05-03
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交