UVa, 10701 Pre, in and post
来源:互联网 发布:微信分享 js 案例 编辑:程序博客网 时间:2024/06/12 00:26
Problem F - Pre, in and post
Time Limit: 1 second
A common problem in data structures is to determine the traversal ofa binary tree. There are three classic ways to do it:
Pre-order: You must visit insequence the root, left subtree and the right subtree.
In-order: You must visitin sequence the left subtree, the root and the right subtree.
Post-order: You must visit insequence the left subtree, the right subtree and the root.
See the picture below:
The pre, in and post-order traversal are, respectively, ABCDEF, CBAEDF and CBEFDA. In this problem, you must compute the post-ordertraversal of a binary tree given its in-order and pre-order traversals.
Input
The input set consists of a positive number C ≤ 2000, thatgives the number of test cases andClines, one for each test case. Each test case starts with a number 1≤N≤ 52, the number of nodes in this binary tree. After, there will betwo stringsS1 and S2 thatdescribe the pre-order and in-order traversal of the tree. Thenodes of the tree are labeled with different characters in the rangea..zand A..Z. The values of N,S1 and S2 are separeted by a blankspace.
Output
For each input set, you should output a line containing the post-ordertransversal for the current tree.
Sample input
33 xYz Yxz3 abc cba6 ABCDEF CBAEDF
Sample output
YzxcbaCBEFDA
Problem setter: Sebastião Alves#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
using namespace std;
///////////////////////////
class Preinpost{
private:
string preorder;
string inorder;
string postorder;
void reconstruct(string p,string i);
public:
//void readcase(){;
void initial(string p,string i)
{
preorder=p;
inorder=i;
postorder.clear();
}
void computing();
void outresult(){cout<<postorder<<endl;}
};
void Preinpost::computing(){
reconstruct(preorder,inorder);
}
void Preinpost::reconstruct(string preorder,string inorder){
int si=preorder.size();
if(si>0){
int p=int(inorder.find(preorder[0]));
reconstruct(preorder.substr(1,p),inorder.substr(0,p));
reconstruct(preorder.substr(p+1,si-p-1),inorder.substr(p+1,si-p-1));
postorder.push_back(preorder[0]);
}
}
int main(){
Preinpost pip;
string p,i;
//cin>>n;
//getchar();
int n,m;
cin >> n;
while(n--){
//int m;
cin>>m;
cin>>p>>i;
//pip.readcase();
pip.initial(p,i);
pip.computing();
pip.outresult();
}
return 0;
}
二叉树 的基本问题 注意下 recontruct 的算法 体会下 递归的函数 的使用,再者就是 输入输出的变换。。。灵活使用 。。。表示因为输入问题wa
了两次,后来 认识到不是算法的问题 ,改了下输入的方法就ac
- UVa, 10701 Pre, in and post
- UVa 10701 - Pre, in and post
- [UVA 10701]Pre,in and post (二叉树)(dfs)
- UVa10701 - Pre, in and post
- Compile, Pre, and Post Linking in AngularJS
- In sum for pre- / In- and Post- order Traversal
- Pre,in,post expression
- Pre- and Post-order Traversals
- [LeetCode] Construct/build binary tree from in-order and post-order/pre-order traversal
- Erwin pre-script and post-script 触发
- Build Actions (pre-build and post-build)
- 1119. Pre- and Post-order Traversals
- pat1191119. Pre- and Post-order Traversals
- PAT1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- PAT--1119. Pre- and Post-order Traversals
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 比特币是个神马玩意儿?
- 开发与研发
- Leetcode:Binary Tree Maximum Path Sum
- <AppML> 架构
- OpenStack的网络管理指南(4)——使用Openstack网络
- UVa, 10701 Pre, in and post
- Django model字段类型
- Windows phone LocalDataBase 数据结构发生变化
- 动态加载js脚本的方法
- UVa, 536 Tree Recovery
- php继承环境多站点apache设置
- poj 1726 Cash Machine(背包+优化)
- Maximum call stack size exceeded异常原因
- 八卦方位