Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14640   Accepted: 9091

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

已知二叉树的前序后序遍历求后序遍历。
代码:
注意in的范围的变化和pre一样
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct Tree{
Tree *l, *r;
char value;
Tree(){
l = NULL;
r = NULL;
}
}Tree;
int find(char c, char *in){
for(int i = ; in[i]; i++){
if(c == in[i]){
return i;
}
}
}
void make(int n, char *pre, char *in, Tree *&t){
if(n <= )return;
int k = find(pre[], in);
t = new Tree(); t->value = pre[];
make(k, pre + , in, t->l);
make(n - - k, pre + k + , in + k + , t->r);
}
void postVisit(Tree *tree){ if(tree->l)
postVisit(tree->l);
if(tree->r)
postVisit(tree->r);
printf("%c", tree->value);
}
int main(){
char pre[],in[];
while(~scanf("%s%s", pre, in)){
Tree t;
make(strlen(pre), pre, in, t.l);
postVisit(t.l);
puts("");
}
return ;
}
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct Tree{
Tree *l, *r;
char value;
Tree(){
l = NULL;
r = NULL;
}
}Tree;
int find(char c, char *in){
for(int i = ; in[i]; i++){
if(c == in[i]){
return i;
}
}
}
Tree* make(int n, char *pre, char *in){
if(n <= )return NULL;
int k = find(pre[], in);
Tree *t = new Tree(); t->value = pre[];
t->l = make(k, pre + , in);
t->r = make(n - - k, pre + k + , in + k + );
return t;
}
void postVisit(Tree *tree){ if(tree->l)
postVisit(tree->l);
if(tree->r)
postVisit(tree->r);
printf("%c", tree->value);
}
int main(){
char pre[],in[];
while(~scanf("%s%s", pre, in)){
Tree *t;
t = make(strlen(pre), pre, in);
postVisit(t);
puts("");
}
return ;
}
 

Tree Recovery(前序中序求后序)的更多相关文章

  1. HDU 1710 二叉树遍历,输入前、中序求后序

    1.HDU  1710  Binary Tree Traversals 2.链接:http://acm.hust.edu.cn/vjudge/problem/33792 3.总结:记录下根结点,再拆分 ...

  2. PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟

    1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...

  3. python实现根据前序与中序求后序

    我就不板门弄斧了求后序 class Tree(): def __init__(self,x): self.value=x self.left=None self.right=None class So ...

  4. UVa 二叉树重建(先序+中序求后序)

    题意是给出先序和中序,求出后序. 先序遍历先访问根结点,通过根结点可以在中序中把序列分为左子树部分和右子树部分,我建了一个栈,因为后序遍历最后访问根结点,所以把每次访问的根结点放入栈中.因为后序遍历先 ...

  5. c++树,知道前序和中序求后序遍历

    经常有面试题就是知道一棵树的前序遍历和中序遍历让你写出后序遍历,这个慢慢画是能画出来的,但是要很快的弄出来还是要懂原理. 首先说一下三种遍历:所谓的前序后序和中序都是遍历时遍历根节点的顺序.子树的话依 ...

  6. 【美国血统 American Heritage 题解】已知前序中序 求后序

    题目: 题目名称:美国血统 American Heritage 题目来源:美国血统 American Heritage ## 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的 ...

  7. ACM题目————已知前序和中序求后序

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...

  8. 二叉排序树的构造 && 二叉树的先序、中序、后序遍历 && 树的括号表示规则

    二叉排序树的中序遍历就是按照关键字的从小到大顺序输出(先序和后序可没有这个顺序) 一.以序列 6 8 5 7 9 3构建二叉排序树: 二叉排序树就是中序遍历之后是有序的: 构造二叉排序树步骤如下: 插 ...

  9. 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

    将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...

随机推荐

  1. [数学-构造矩阵]NEFU 1113

    依据题意.我已经推导出tn的公式.ti=ti.a+ti.b,ti.a=5*t(i-1).a+4*t(i-1).b,ti.b=t(i-1).a+t(i-1).b 然而以下居然不能继续推到sn的公式!!! ...

  2. js 添加天数

    //日期加上天数得到新的日期 //dateTemp 需要参加计算的日期,days要添加的天数,返回新的日期,日期格式:YYYY-MM-DD function getNewDay(dateTemp, d ...

  3. UMEditor(Ueditor mini)修改图片上传路径

    UMEditor(Ueditor mini)修改图片上传路径 imageUp.ashx string pathbase = "/UpLoad/images/"; //保存文件夹在网 ...

  4. 微信小程序——网盘图片预览

    微信小程序图片预览提供了一个wx.previewImage接口,如下图: 现在我需要对网盘文件里的图片预览,但是网盘从后台返回的数据是各种类型的文件,如下图所示: 那么我们需要解决2个问题: 1.从这 ...

  5. windows 下安装 redis

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  6. Android Studio apk 打包

    1.Build -> Generate Signed APK...,打开如下窗口 2.假设这里没有打过apk包,点击Create new,窗口如下 这里只要输入几个必要项 Key store p ...

  7. python 查看目录下所有目录和文件

    python查看目录下所有的子目录和子文件 python递归遍历目录结构 我喜欢第一种 方法1 import json, os def list_dir(path, res): for i in os ...

  8. C# 使用XPath解析网页

    1.需要安装库HtmlAgilityPack ,官网http://htmlagilitypack.codeplex.com/ // From File var doc = new HtmlDocume ...

  9. Java如何暂停线程一段时间?

    在Java编程中,如何暂停线程一段时间? 以下示例显示如何通过创建sleepThread()方法来暂停线程一段时间. package com.yiibai; public class Suspendi ...

  10. Java获取正在执行的函数名

    利用StackTrace堆栈轨迹获取某个时间的调用堆栈状态. package com.dsp.demo; public class TechDemo { public static void main ...