程序填空题:先序+中序创建二叉树
已知先序遍历序列和中序遍历序列建立二叉树。
例如

输入先序遍历序列:
ABDFGC,
再输入中序遍历序列:
BFDGAC,则
输出该二叉树的后序遍历序列:
FGDBCA。
```c++
#include
#include
#include
typedef char ElementType;
typedef struct BiTNode{
ElementType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
BiTree CreatBinTree(char *pre,char*in,int n );
void postorder( BiTree T );
int main()
{
BiTree T;
char prelist[100];
char inlist[100];
int length;
scanf("%s",prelist);
scanf("%s",inlist);
length=strlen(prelist);
T=CreatBinTree(prelist,inlist, length);
postorder( T );
return 0;
}
void postorder( BiTree T )
{
if(T)
{
postorder(T->lchild);
postorder(T->rchild);
printf("%c",T->data);
}
}
BiTree CreatBinTree(char *pre,char*in,int n)
{
BiTree T;
int i;
if(n<=0) return NULL;
T=(BiTree)malloc(sizeof(BiTNode));
T->data=pre[0];
for(i=0;in[i]!=pre[0];i++);
T->lchild=@@[CreatBinTree(pre+1,in,i)](3);
T->rchild=@@[CreatBinTree(pre+i+1,in+i+1,n-i-1)](3);
return T;
}
```
答案:
第1空:CreatBinTree(pre+1,in,i)
第2空:CreatBinTree(pre+i+1,in+i+1,n-i-1)
例如

输入先序遍历序列:
ABDFGC,
再输入中序遍历序列:
BFDGAC,则
输出该二叉树的后序遍历序列:
FGDBCA。
```c++
#include
#include
#include
typedef char ElementType;
typedef struct BiTNode{
ElementType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
BiTree CreatBinTree(char *pre,char*in,int n );
void postorder( BiTree T );
int main()
{
BiTree T;
char prelist[100];
char inlist[100];
int length;
scanf("%s",prelist);
scanf("%s",inlist);
length=strlen(prelist);
T=CreatBinTree(prelist,inlist, length);
postorder( T );
return 0;
}
void postorder( BiTree T )
{
if(T)
{
postorder(T->lchild);
postorder(T->rchild);
printf("%c",T->data);
}
}
BiTree CreatBinTree(char *pre,char*in,int n)
{
BiTree T;
int i;
if(n<=0) return NULL;
T=(BiTree)malloc(sizeof(BiTNode));
T->data=pre[0];
for(i=0;in[i]!=pre[0];i++);
T->lchild=@@[CreatBinTree(pre+1,in,i)](3);
T->rchild=@@[CreatBinTree(pre+i+1,in+i+1,n-i-1)](3);
return T;
}
```
答案:
第1空:CreatBinTree(pre+1,in,i)
第2空:CreatBinTree(pre+i+1,in+i+1,n-i-1)