数据结构上机测试4.1:二叉树的遍历与应用1

数据结构上机测试4.1:二叉树的遍历与应用1

Description

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

Input

第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。

Output

输出该二叉树的后序遍历序列。

Sample

Input 

ABDCEF
BDAECF

Output 

DBEFCA
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 typedef struct node
 5 {
 6     char data;
 7     struct node *lc,*rc;
 8 }bitree;
 9 bitree * creat(int len,char qx[51],char zx[51])
10 {
11     int i;
12     bitree *t;
13     if(len<=0)
14         return NULL;
15     t=(bitree *)malloc(sizeof(bitree));
16     t->data=qx[0];
17     for(i=0;i<len;i++)
18     {
19         if(zx[i]==qx[0])
20             break;
21     }
22     t->lc=creat(i,qx+1,zx);
23     t->rc=creat(len-i-1,qx+i+1,zx+i+1);
24     return t;
25 }
26 void postshow(bitree * tree)
27 {
28     bitree *t;
29     t=tree;
30     if(t)
31     {
32         postshow(t->lc);
33         postshow(t->rc);
34         printf("%c",t->data);
35     }
36 }
37 int main()
38 {
39     int len;
40     char qx[51],zx[51];
41     bitree *tree;
42     scanf("%s%s",qx,zx);
43     len = strlen (zx);
44     tree=creat(len,qx,zx);
45     postshow(tree);
46     printf("\n");
47     return 0;
48 }

 

posted on 2020-04-10 21:38  llllIYIlIN  阅读(182)  评论(0编辑  收藏  举报

导航