程序员王斌爱玩原神 • 1个月前
using namespace std; typedef struct node {
char data;
node *lchild, *rchild;
} tree,*treelink; void creat_tree(treelink&t,string pre,string in){
if(pre.size()==0){
t=NULL;
return;
}
char root=pre[0];
int index=in.find(root);
string left_in=in.substr(0,index);
string right_in=in.substr(index+1);
int len_left=left_in.size();
int len_right=right_in.length();
string left_pre=pre.substr(1,len_left);
string right_pre=pre.substr(len_left+1);
t=new tree;
t->data=root;
creat_tree(t->lchild,left_pre,left_in);
creat_tree(t->rchild,right_pre,right_in);
} void last(treelink t){
if(t!=NULL){
last(t->lchild);
last(t->rchild);
cout<<t->data;
}
} int main(){
string pre,in;
cin>>pre>>in;
treelink t;
creat_tree(t,pre,in);
last(t);
return 0;
}
评论:
请先登录,才能进行评论