2015 HUAS Summer Training#2~D

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 Specification 

The input file 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 Specification 

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
解题思路:根据先序的特点可以知道第一个字母是这棵树的根,利用递归调用可以求出来,但是要小心不是树的条件。
程序代码:
#include <stdio.h> #include<string.h> int find(char c,char A[],int s,int e) /* 找出中序中根的位置。 */ { int i; for(i=s;i<=e;i++) if(A[i]==c) return i; return 0; } /* 其中pre[]表示先序序,pre_s为先序的起始位置,pre_e为先序的终止位置。 */ /* 其中in[]表示中序,in_s为中序的起始位置,in_e为中序的终止位置。 */ /* pronum()求出pre[pre_s~pre_e]、in[in_s~in_e]构成的后序序列。 */ void pronum(char pre[],int pre_s,int pre_e,char in[],int in_s,int in_e) { char c; int k; if(in_s>in_e) return ; /* 非法子树,完成。 */ if(in_s==in_e){printf("%c",in[in_s]); /* 子树子仅为一个节点时直接输出并完成。 */ return ; } c=pre[pre_s]; /* c储存根节点。 */ k=find(c,in,in_s,in_e); /* 在中序中找出根节点的位置。 */ pronum(pre,pre_s+1,pre_s+k-in_s,in,in_s,k-1); /* 递归求解分割的左子树。 */ pronum(pre,pre_s+k-in_s+1,pre_e,in,k+1,in_e); /* 递归求解分割的右子树。 */ printf("%c",c); /* 根节点输出。 */ } int main() { char a[26],b[26]; //string *a,*b; while(scanf("%s",&a)&&scanf("%s",&b)!=EOF) { pronum(a,0,strlen(b)-1,b,0,strlen(a)-1); printf("\n"); } return 0; } 

你可能感兴趣的:(ini)