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

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

                                                                                                               Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9710   Accepted: 6105

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

ACBFGEDCDAB 转:
#include 
using namespace std;char pre[100];//先序遍历char in[100];//中序遍历char post[100];//后序遍历int len;//节点个数void solve(int p1,int p2,int m1,int m2){ if(p1>p2) return ; int i; for(i=m1;i<=m2;i++) { if(in[i]==pre[p1]) break; } post[--len]=pre[p1];//根,放到后序遍历的最后面 if(p1==p2)//叶子节点 return ; solve(p1+i-m1+1,p2,i+1,m2);//递归处理右子树,得到右子树后序遍历 solve(p1+1,p1+i-m1,m1,i-1);//处理左子树,得到左子树后序遍历}int main(){ while (cin>>pre>>in) { memset(post,0,sizeof(post)); len=strlen(pre); solve(0,len-1,0,len-1); cout<
<

 

转载于:https://www.cnblogs.com/Deng1185246160/p/3239053.html

你可能感兴趣的文章
如何开启safari的调试
查看>>
js深拷贝和浅拷贝
查看>>
node.js 基础学习笔记1
查看>>
如何在linux系统中设置静态ip地址
查看>>
二分查找法,折半查找原理
查看>>
DP简单问题联系--最长递增子序列+最长公共子序列等
查看>>
2017-4-18 Zabbix server的安装以及ansible批量部署zabbix agent的工作笔记
查看>>
GridView 动态列上方添加相应的Combox等控件
查看>>
申请开发者账号
查看>>
oracle启动
查看>>
c++模板学习
查看>>
【转】MySQL Event
查看>>
[转]html5监听任何App自带返回键javascript事件
查看>>
通俗理解LDA主题模型
查看>>
jzoj5813
查看>>
HttpServletRequest 获取URL的方法及区别
查看>>
VMware环境和Window环境进行网络连接的问题
查看>>
macOS10.12允许所有来源设置
查看>>
C++有关 const & 内敛 & 友元&静态成员那些事
查看>>
函数积累
查看>>