♻️lzhh_lzhh32 • 3个月前
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
void maketree(int left1,int right1,int left2,int right2){ //1是先序遍历,2是中序遍历。left是左端点,right是右端点
char root=s1[left1]; //left1~right1为先序序列,left2~right2为中序序列
int find;
for(find=left2;find<=right2;find++){ //在中序遍历中找到根节点
if(s2[find]==root){
maketree(left1+1, left1+find-left2, left2, find-1); //左子树
maketree(right1-(right2-find-1), right1, find+1, right2); //右子树
cout<<root;
}
}
}
int main(){
cin>>s1>>s2;
maketree(0,s1.size()-1,0,s2.size()-1);
return 0;
}
评论:
请先登录,才能进行评论