//由先序遍历和中序遍历得到后序遍历
//算法思想
//首先由先序遍历的到根节点
//然后分成左子树和右子树
//把先序遍历的第一个给后序遍历的最后一个
#include <iostream>
#include <cstring>
using namespace std;
void Post(int len,char *pre,char *in,char *post)
{
//如果长度小于等于0的话,返回
if(len>0)
{
//前序遍历的第一个来分开左子树和右子树
int pos = strchr(in,pre[0])-in;
//左子树
Post(pos,pre+1,in,post);
//右子树
Post(len-pos-1,pre+pos+1,in+pos+1,post+pos);
post[len-1] = pre[0];
}
}
int main()
{
char *pre = "abdhecfg";
char *in = "hdbeafcg";
char *post;
post = new char[20];
Post(strlen(pre),pre,in,post);
cout<<post;
post[len] = '\0';
return 0;
}
//算法思想
//首先由先序遍历的到根节点
//然后分成左子树和右子树
//把先序遍历的第一个给后序遍历的最后一个
#include <iostream>
#include <cstring>
using namespace std;
void Post(int len,char *pre,char *in,char *post)
{
//如果长度小于等于0的话,返回
if(len>0)
{
//前序遍历的第一个来分开左子树和右子树
int pos = strchr(in,pre[0])-in;
//左子树
Post(pos,pre+1,in,post);
//右子树
Post(len-pos-1,pre+pos+1,in+pos+1,post+pos);
post[len-1] = pre[0];
}
}
int main()
{
char *pre = "abdhecfg";
char *in = "hdbeafcg";
char *post;
post = new char[20];
Post(strlen(pre),pre,in,post);
cout<<post;
post[len] = '\0';
return 0;
}