#include<iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef struct node{
char data;
struct node *lchild,*rchild;
}BTNode;
void CreateRoot(BTNode *&root,string post,string in){
if(post.empty()) return ;
root = new BTNode();
root->data = *(post.end() - 1);
int pos = in.find(*(post.end() - 1));///string函数
string il,ir,pl,pr;
il = in.substr(0,pos);
ir = in.substr(pos+1);
pl = post.substr(0,pos);
pr = post.substr(pos,ir.length());
//cout<<il<<endl<<ir<<endl<<pl<<endl<<pr<<endl;
CreateRoot(root->lchild,pl,il);
CreateRoot(root->rchild,pr,ir);
}
void pre(BTNode *root){
if(!root) return ;
cout<<root->data;
pre(root->lchild);
pre(root->rchild);
}
int main(){
string p,m;
cin>>p>>m;
BTNode *root;
CreateRoot(root,p,m);
pre(root);
cout<<endl;
return 0;
}
【递归】已知中序遍历,后序遍历,求前序遍历
最新推荐文章于 2024-06-27 19:32:22 发布