二叉树重建

输入一棵二叉树的先序遍历和中序遍历序列,输出它的后序遍历序列。


#include <iostream>
#include <string.h>
using namespace std;

char s[100];

void build(int n, char *s1, char *s2, char *s)
{
	if (n <= 0)
	{
		return;
	}

	int p = strchr(s2, s1[0]) - s2;
	build(p, s1+1, s2, s);
	build(n-p-1, s1+p+1, s2+p+1, s+p);
	s[n-1] = s1[0];
}
int main()
{
	char s1[] = "abdec";
	char s2[] = "dbeac";

	int n = strlen(s1);
	build(n, s1, s2, s);
	s[n] = '\0';
	cout << s << endl;

	return 0;
}
 


你可能感兴趣的:(二叉树重建)