文章
40
粉丝
512
获赞
13
访问
371.8k
#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;
}
}
登录后发布评论
暂无评论,来抢沙发