POJ 2255 Tree Recovery 树的遍历 水题
来源:互联网 发布:js 弹出输入框 编辑:程序博客网 时间:2024/06/06 11:04
Tree RecoveryTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13969 Accepted: 8716DescriptionLittle 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 / / FTo 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!InputThe 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.OutputFor each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).Sample InputDBACEGF ABCDEFGBCAD CBADSample OutputACBFGEDCDABSourceUlm Local 1997
前序遍历+中序遍历 求 后序遍历
#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>char pre[30];char in[30];void f(int sPre,int ePre,int sIn,int eIn){ if(sPre>ePre) return; char root=pre[sPre];//前序遍历第一个节点是树根 for(int i=sIn;i<=eIn;++i){//寻找树根在中序遍历中的位置 if(in[i]==root){ int lenLeft=i-sIn; f(sPre+1,sPre+lenLeft,sIn,i-1);//递归输出左子树 int lenRight=eIn-i; f(sPre+lenLeft+1,sPre+lenLeft+lenRight,i+1,eIn);//右 break; } } putchar(root);//输出根节点}int main(){ //freopen("/home/lu/文档/r.txt","r",stdin); //freopen("/home/lu/文档/w.txt","w",stdout); while(~scanf("%s%s",pre,in)){ int len=strlen(pre); f(0,len-1,0,len-1); putchar('\n'); } return 0;}
0 0
- POJ 2255 Tree Recovery 树的遍历 水题
- POJ 2255 Tree Recovery 二叉树遍历
- POJ 2255 Tree Recovery (二叉树遍历)
- Poj 2255 Tree Recovery【dfs遍历树】
- poj 2255 Tree Recovery 树的遍历 简单string用法
- POJ 2255 Tree Recovery 二叉树的遍历
- poj 2255 Tree Recovery(二叉树的遍历)
- POJ 2255 Tree Recovery(树的遍历)
- poj 2255 Tree Recovery (二叉树的顺序遍历)
- POJ 2255 Tree Recovery 二叉树+遍历+递归
- poj 2255 Tree Recovery(求后序遍历,二叉树)
- POJ 2255 Tree Recovery(二叉树遍历)
- POJ 2255/hrbust 2022 Tree Recovery【dfs、二叉树的层次遍历】
- POJ 2255 Tree Recovery(根据前中序遍历,求后序遍历)
- poj2255~~Tree Recovery (树的遍历构造~)
- POJ2255 Tree Recovery 【树的遍历】
- poj 2255 Tree Recovery
- Tree Recovery---POJ--2255
- Android Studio中Gradle介绍
- android studio File Explorer中查看不到SharedPreferences保存的文件
- codis3.1 codis-test 安装部署实践之一 zookeeper 安装部署实践
- 转换已经存在的java项目,使用gradle构建
- Effective Java笔记
- POJ 2255 Tree Recovery 树的遍历 水题
- JavaScript循环语句的性能问题[旧]
- codis3.1 codis-test 安装部署实践之二 编译安装codis
- jQuery 应用案例1——table隔行变色
- JavaScript面向对象编程之封装(一)
- 240. Search a 2D Matrix II
- RHEL7配置
- 获取本地IP地址的vc代码
- Xcode里修改已创建类的名字