一道树的题目
#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<sstream>
#include<cctype>
#include<queue>
using namespace std;
struct node
{
char data;
node*left;
node*right;
};
node* build(string s)
{
stack<node *>st;
for(int i=0;i<s.size();i++){
if(isupper(s[i])){
node *right = st.top();st.pop();
node *left = st.top();st.pop();
node *nod = new node;
nod->left=left;nod->right=right;
nod->data =s[i];
st.push(nod);
}
else{
node* nod=new node;
nod->left=0;nod->right=0;
nod->data = s[i];
st.push(nod);
}
}
return st.top();
}
string get_ans(node* root)
{
queue<node*>q;
q.push(root);
string ans;
while(!q.empty())
{
node*cur=q.front();q.pop();
ans+=cur->data;
if(cur->left)q.push(cur->left);
if(cur->right)q.push(cur->right);
}
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
int n;
string s;
cin>>n;
while(n--)
{
string ans;
cin>>s;
node* root=build(s);
cout<<get_ans(root)<<endl;
}
return 0;
}