Tree Recovery(由先、中序列构建二叉树)

题目来源:

http://poj.org/problem?id=2255

题目描述:

Description

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:

D
/
/
B E
/
/
A C G
/
/
F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!

Input

The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.

Output

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

解题思路:
根据给出的先根序列和中根序列构建二叉树后,后序遍历二叉树即可。
拿第一个样例来说,现在先根序列中找到根节点D,然后在中根序列中找到D,可以得到以D为根节点的二叉树的左子树有ABC,以D为根节点的二叉树的右子树有EFG。接下来在先根序列中找到B,再在中根序列中将以B为根节点的二叉树
的左右子树找到,如此递归,直至将序列处理完毕。
代码实现:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 struct node{
 5     char ch;
 6     struct node *left,*right;
 7 };
 8 struct node* creat(char *pre,char *in,int len); 
 9 void post(struct node* head);
10 int main()
11 {
12     char pre[30],in[30];
13     struct node *head;
14     head = ( struct node *)malloc(sizeof(struct node));
15     while(scanf("%s %s",pre,in) != EOF)
16     {
17         int len=strlen(pre);
18         head=creat(pre,in,len);
19         
20         post(head);//后序遍历
21         printf("
");
22     }
23     return 0;
24 }
25 struct node* creat(char *pre,char *in,int len)
26 {
27     if(len==0)
28         return NULL;
29         
30     struct node *head;
31     head = (struct node*)malloc(sizeof(struct node));    
32     head->ch=pre[0];
33     
34     char *p;
35     for(p=in;p != NULL;p++)//指针字符串中空为结束标志 
36         if(*p == *pre)
37             break;
38             
39     int k=p-in;
40     head->left=creat(pre+1,in,k);
41     head->right=creat(pre+k+1,p+1,len-k-1);
42     return head; 
43 }
44 void post(struct node* head)
45 {
46     if(head == NULL)
47         return;
48     post(head->left);
49     post(head->right);
50     printf("%c",head->ch);
51 } 

易错分析:

注意注释,指针字符串和字符串数组还是有一定区别的,比如结束标志位NULL

原文地址:https://www.cnblogs.com/wenzhixin/p/8343860.html