博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ_2255_Tree Recovery
阅读量:6249 次
发布时间:2019-06-22

本文共 2495 字,大约阅读时间需要 8 分钟。

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12342   Accepted: 7712

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 ABCDEFGBCAD CBAD

Sample Output

ACBFGEDCDAB3 题解:在前序中取根节点,在中序中找到该节点,其左边为该根节点左子树,右边为右子树 代码:
#include
#include
#include
using namespace std;char fro[27],mid[27];void beh(int fs,int fe,int ms,int me){ if(fs==fe) { cout<
fe||ms>me) return; char root=fro[fs]; int i; for(i=ms;i

 

转载于:https://www.cnblogs.com/jasonlixuetao/p/4534314.html

你可能感兴趣的文章
javascript练习:8-10事件与this运算符
查看>>
Linux下SVN部署/安全及权限配置,实现web同步更新
查看>>
PHPSPY2013
查看>>
Android学习笔记(四)时钟、时间
查看>>
SQL SERVER 查询性能优化——分析事务与锁(二)
查看>>
动画实现实现上下滚动的TextView
查看>>
HDU-4461 The Power of Xiangqi 签到题
查看>>
方法线程SwingWorker的用法
查看>>
hdu 4313(类似于kruskal)
查看>>
【数据存储】数据查询与Cursor接口(4)
查看>>
DoTA与人生
查看>>
〖Android〗/system/etc/media_codecs.xml
查看>>
ESN 与 MEID
查看>>
MySQL server version for the right syntax to use near ‘USING BTREE
查看>>
launch failed.Binary not found
查看>>
MySql服务器的启动和关闭
查看>>
Android访问远程网页取回json数据
查看>>
Android之等比例显示图片
查看>>
HTML5 data-* 自定义属性
查看>>
linux系统装windows时需要注意的问题
查看>>