#include <iostream>
using namespace std;
void pos(char* inorder, char* preorder, int length)
{
if(length == 0)
return;
char c = *preorder;
int rootIndex = 0;
for(;rootIndex < length; rootIndex++)
{
if(inorder[rootIndex] == *preorder)
break;
}
pos(inorder, preorder +1, rootIndex);
pos(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));
cout << c << endl;
return;
}
void pre(char* inorder, char* postorder, int length)
{
if(length == 0)
return;
char c = *(postorder+length-1);
int rootIndex = 0;
for(;rootIndex < length; rootIndex++)
{
if(inorder[rootIndex] == c)
break;
}
cout << c << endl;
pre(inorder, postorder, rootIndex);
pre(inorder + rootIndex + 1, postorder + rootIndex, length - (rootIndex + 1) );
return;
}
int main()
{
char* pre="GDAFEMHZ";
char* ino="ADEFGHMZ";
char* pos="AEFDHZMG";
pos(in, pr, 8);
pre(in, po, 8);
return 0;
}