后序遍历最后一个是根节点,还是根据后序遍历,不断在中序遍历中找根,切割区分左右子树
其实用dfs就可以做
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
char in[10],post[10];
char lchild,rchild,root;
char dfs(int L1,int R1,int L2,int R2)
{
if(L1>R1)
return 0;
root=post[R2];
int loc=L1;
while(in[loc]!=root)
loc++;
int num=loc-L1;
cout<<root;
lchild=dfs(L1,loc-1,L2,L2+num-1);
rchild=dfs(loc+1,R1,L2+num,R2-1);
return root;
}
int main()
{
scanf("%s%s",in,post);
int len=strlen(in);
dfs(0,len-1,0,len-1);
return 0;
}