- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
哈工程网上ACM测试题及答案
哈尔滨工程大学ACM测试题及答案
1045:
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 Valentines binary tree and print one line containing the trees postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFGBCAD CBAD
Sample Output
ACBFGEDCDAB
#includestdio.h
#includestring.h
char pre[30],in[30];
void recover(int pi,int pj,int ii,int ij)
{
? ? if(pi+1pj)
? ? ? ? return;
? ? int i=ii;
? ? while(in[i]!=pre[pi])
? ? ? ? i++;
? ? for(int j=pi; jpj-1; j++)
? ? ? ? pre[j]=pre[j+1];
? ? pre[pj-1]=in[i];
? ? recover(pi,pi+i-ii,ii,i);
? ? recover(pi+i-i
文档评论(0)