POJ 2255 Tree Recovery 树的遍历 水题
2016-10-22 17:33
369 查看
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13969 Accepted: 8716 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 ABCDEFG BCAD CBAD Sample Output ACBFGED CDAB Source Ulm 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; }
相关文章推荐
- POJ 2255 Tree Recovery 二叉树的遍历
- POJ 2255 Tree Recovery 树的遍历,分治 难度:0
- POJ2255 Tree Recovery 【树的遍历】
- poj 2255 Tree Recovery 树的遍历 简单string用法
- POJ 2255/hrbust 2022 Tree Recovery【dfs、二叉树的层次遍历】
- POJ 2255 Tree Recovery 二叉树的遍历
- POJ 2255 Tree Recovery 笔记
- POJ 2255 Tree Recovery 二叉树恢复
- poj_2255_Tree Recovery_解题报告
- Poj 2255 Tree Recovery【dfs遍历树】
- POJ 2255 Tree Recovery [二叉树]
- POJ 2255 Tree Recovery 二叉树
- POJ 2255 Tree Recovery解题报告
- POJ 2255 Tree Recovery 二叉树恢复
- poj2255 Tree Recovery 字典树 解题报告(Ulm Local 1997)
- Tree Recovery---POJ--2255
- B - Tree Recovery POJ - 2255 (二叉树)
- POJ 2255 Tree Recovery【二叉树重建】
- poj 2255 Tree Recovery 递归
- POJ 2255 Tree Recovery lightblueme