1086 Tree Traversals Again(25 分)

#include
using namespace std;
struct node{
    int data;
    node *lchild,*rchild;
};
vectorpre,in,post;
stackq;
node*create(int preL,int preR,int inL,int inR)
{
    if(preL>preR)return NULL;
    node*root=new node;
    root->data=pre[preL];
    int k;
    for(k=inL;k<=inR;k++)
    {
        if(in[k]==root->data)break;
    }
    int numleft=k-inL;
    root->lchild=create(preL+1,preL+numleft,inL,k-1);
    root->rchild=create(preL+numleft+1,preR,k+1,inR);
    return root;
}
void postorder(node*root)
{
    if(root==NULL)return;
    postorder(root->lchild);
    postorder(root->rchild);
    post.push_back(root->data);
}
int n;
int main()
{
    scanf("%d",&n);
    int k=2*n;
    while(k--)
    {
        getchar();
        string s;
        int x;
        cin>>s;
        if(s[1]=='u')
        {
            scanf("%d",&x);
            pre.push_back(x);
            q.push(x);
        }
        else 
        {
            x=q.top();
            q.pop();
            in.push_back(x);
        }
    }
    node*root=create(0,n-1,0,n-1);
    postorder(root);
    for(int i=0;i

你可能感兴趣的:(1086 Tree Traversals Again(25 分))