比如前序:GDAFEMHZ,中序:ADEFGHMZ,根据前序来划分中序,首先确定根节点,就是先序遍历中的第一个元素,然后用该元素区划分中序序列,分成了两个序列,分别是:ADEF和HMZ,其中ADEF是位于G得左子树中,HMZ位于G的右子树中。然后同理对ADEF进行同样的划分(划分节点为D),就这样不断递归下去,在递归返回时,注意要返回当前节点的地址。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
struct node
{
char str;
node *left;
node *right;
node(){
}
};
int x=0;
node* solve(string s1,string s2)
{
if(s2.length()==1) //到达根节点
{
node* point=new node();
point->str=s2[0];
point->left=NULL;
point->right=NULL;
//cout<<point->str<<" ";
return point;
}
string x1,x2;
int len=s2.length();
int i;
for(i=0;i<=len;i++)
{
if(s1[x]!=s2[i])
x1+=s2[i];
else
break;
}
i++;
for(;i<len;i++)
x2+=s2[i];
node *point=new node();
point->left=NULL;
point->right=NULL;
point->str=s1[x];
if(x1.length()!=0) //构建左子树
{
x++;
point->left=solve(s1,x1);
}
if(x2.length()!=0) //构建右子树
{
x++;
point->right=solve(s1,x2);
}
//cout<<point->str<<" ";
return point;
}
void dfs(node* point)
{
if(point->left!=NULL)
dfs(point->left);
if(point->right!=NULL)
dfs(point->right);
printf("%c",point->str);
}
int main()
{
string s1,s2;
cin>>s1>>s2;
node* root=NULL;
root=solve(s1,s2);
dfs(root);
printf("\n");
return 0;
}
/*
GDAFEMHZ
ADEFGHMZ
*/