#include<stdio.h>
#include<string.h>
char
pre[26];
char
in[26];
char
post[26];
void
Post(
int
low1,
int
high1,
int
low2,
int
high2,
int
low,
int
high)
{
if
(low>high)
return
;
char
c=pre[low1];
post[high]=c;
int
k=0;
while
(in[low2+k]!=c)
k++;
Post(low1+1,low1+k,low2,low2+k-1,low,low+k-1);
Post(low1+k+1,high1,low2+k+1,high2,low+k,high-1);
return
;
}
int
main()
{
while
(
scanf
(
"%s"
,pre)!=EOF)
{
scanf
(
"
%s"
,in);
int
len=
strlen
(pre);
Post(0,len-1,0,len-1,0,len-1);
printf
(
"%s"
,post);
printf
(
"\n"
);
}
return
0;
}
#include
<iostream>
#include
<cstring>
using
namespace
std;
typedef
struct
BinaryTree
{
char
data;
BinaryTree*
lchild;
BinaryTree*
rchild;
}BinaryTree;
void
RebuildTree(BinaryTree* &Tree, char *
pre, char *
in, int
len)
{
Tree
= (BinaryTree*) malloc ( sizeof (BinaryTree));
if (Tree!=NULL)
{
if (len<=0)
{
Tree
= NULL;
return
;
}
int
index = 0;
while (index<len&&*(pre)!=*(in+index))
{
index++;
}
Tree->data
= *(pre);
RebuildTree(Tree->lchild,pre+1,in,index);
RebuildTree(Tree->rchild,pre+1+index,in+1+index,len-(index+1));
}
return
;
}
void
PostOrderTravese(BinaryTree* Tree)
{
if (Tree==NULL)
return ;
PostOrderTravese(Tree->lchild);
PostOrderTravese(Tree->rchild);
printf ( "%c" ,Tree->data);
}
int
main()
{
char
pre[101];
char
in[101];
while ( scanf ( "%s
%s" ,pre,in)!=EOF)
{
BinaryTree*
tree;
int
length = strlen (pre);
RebuildTree(tree,pre,in,length);
PostOrderTravese(tree);
cout<<endl;
}
return
0;
}
|