文章

40

粉丝

512

获赞

13

访问

354.8k

头像
样版题
Ang VIP
P1561 北京邮电大学2017年计算机考研机试试题
发布于2020年3月10日 23:12
阅读数 8.2k

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

struct node{
    char data;
    node* l;
    node* r;
    node(char d):data(d),l(NULL),r(NULL){}
};

node* Build(string pre,string mid){
    if(pre.size()==0){
        return NULL;
    }
    char c=pre[0];
    int pos=mid.find(c);
    node* root = new node(c);
    root->l=Build(pre.substr(1,pos),mid.substr(0,pos));
    root->r=Build(pre.substr(pos+1),mid.substr(pos+1));
    return root;
}

void PostOrder(node* root){
    if(root==NULL){
        return;
    }
    PostOrder(root->l);
    PostOrder(root->r);
    cout<<root->data;
    return;
}

int main(){
    string pre,mid;
    while(cin>>pre>>mid){
        node* root=Build(pre,mid);
        PostOrder(root);
        cout<<endl;
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发