题目:通过输入二叉树先序遍历和中序遍历,输出其后序遍历
输入样例:FDXEAG
XDEFAG
输出样例:XEDGAF
实现代码:
#include <stdio.h>
#include <string.h>
struct Node{
Node *lchild;
Node *rchild;
char c;
}Tree[50];
int loc;
Node *create(){
Tree[loc].lchild=Tree[loc].rchild=NULL;
return &Tree[loc++];
}
char str1[30],str2[30];
void PostOrder(Node *node){
if(node->lchild!=NULL){
PostOrder(node->lchild);
}
if(node->rchild!=NULL){
PostOrder(node->rchild);
}
printf("%c",node->c);
}
Node *Build(int s1,int e1,int s2,int e2){
Node *T=create();
int rootIDx=0;
T->c=str1[s1];
for(int i=s2;i<=e2;i++){
if(str1[s1]==str2[i]){
rootIDx=i;
break;
}
}
if(rootIDx!=s2){
T->lchild=Build(s1+1,s1+(rootIDx-s2),s2,rootIDx-1);
}
if(rootIDx!=e2){
T->rchild=Build(s1+(rootIDx-s2)+1,e1,1+rootIDx,e2);
}
return