#include <cstdio> #include <stack> #include <queue> #include <cstdlib> #include <cstring> using namespace std; const int N = 10005; struct Node { char c; Node* left; Node* right; Node() { left = right = NULL; } }; char ans[N]; stack<Node*> s; void bfs() { int front = 0, rear= 1; int n = 0; Node* q[N]; Node* root = s.top(); q[0] = root; while (front < rear) { Node* u = q[front++]; ans[n++] = u->c; if (u->right != NULL) q[rear++] = u->right; if (u->left != NULL) q[rear++] = u->left; } return; } int main() { int t, n; char a[N]; scanf("%d", &t); while (t--) { scanf("%s", a); n = strlen(a); for (int i = 0; i < n; i++) { if (a[i] <= 'z' && a[i] >= 'a') { Node* u = (Node*) malloc(sizeof(Node)); u->left = u->right = NULL; u->c = a[i]; s.push(u); } else { Node* u = (Node*) malloc(sizeof(Node)); u->left = s.top(); s.pop(); u->right = s.top(); s.pop(); u->c = a[i]; s.push(u); } } bfs(); for (int i = n - 1; i >= 0; i--) printf("%c", ans[i]); printf("\n"); while (!s.empty()) s.pop(); } return 0; }
原文地址:http://blog.csdn.net/hyczms/article/details/38044257