- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
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-ii,pj
您可能关注的文档
最近下载
- 托育服务人员的培训与教育.docx
- 冀人版-第11课 电在我家(教学课件).pptx VIP
- 食品安全突发事件应急处置培训课件.pptx VIP
- 2025至2030中国电镀行业发展空间与未来投资走势预测报告.docx VIP
- 汽车智能底盘原理及技术 第3章 线控制动系统.ppt VIP
- 大学课程《工程制图与识图》PPT课件:第三章 基本体的投影 第一节 平面体的投影.pptx VIP
- FANUC数控系统0i-F Plus维修说明书(非常详细).pdf VIP
- 中医基础理论-体质学说 赖伟.ppt VIP
- 生产组织培训课件.pptx VIP
- 工业机器人离线编程与仿真(ABB)高职全套教学课件.pptx VIP
文档评论(0)