2015 HUAS Summer Training#2~D
2015-07-24 21:29
393 查看
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
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:
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 is terminated by end of file.
程序代码:
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
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; }
相关文章推荐
- Project Euler:Problem 90 Cube digit pairs
- 2015 HUAS Summer Training#2 G
- 2015 HUAS Summer Training#2 F
- 2015 HUAS Summer Training#2 E
- 2015 HUAS Summer Training#2 D
- 2015 HUAS Summer Training#2 C
- 狡猾的百度安全组件、百度安全防护服务
- 2015 HUAS Summer Training#2 B
- hdu 5307 He is Flying 2015 Multi-University Training Contest 2 快速傅里叶变换
- Hdu 5302 Connect the Graph 2015 Multi-University Training Contest 2
- 利用standford-nlp库实现Naive Bayes文本分类系统
- 【CODEFORCES】 A. Dreamoon and Stairs
- 更换STM32芯片类型引起keil下载Error Flash download failed-Cortex-M3问题的解决
- NSURLErrorDomain错误码
- HDU 5305 Friends 2015 Multi-University Training Contest 2 1006
- failed to open stream: HTTP wrapper does not support writeable connections
- 2015 HUAS Summer Training#2 A
- zoj1713 Haiku Review
- 使用CURL出现certificate verify failed错误的解决方法
- Assertion failed (ni > 0 && ni == ni1) in collectCalibrationData