UVa 536 Tree Recovery(二叉树后序遍历)
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
题意
已知先序和中序,求后序
题解
这题是处理字符,跟处理数字类似,改一点点
代码
#include<bits/stdc++.h>
using namespace std;
char Per[],In[];
int Left[],Right[];
int rebuild(int L1,int R1,int L2,int R2)
{
if(L1>R1)return ;
int root=Per[L1]-'A'+;
int p=L2;
while(In[p]-'A'+!=root)p++;
int cnt=p-L2;
Left[root]=rebuild(L1+,L1+cnt,L2,p-);
Right[root]=rebuild(L1+cnt+,R1,p+,R2);
return root;
}
void dfs(int u)
{
//printf("%c",u+'A'-1);//先序
if(Left[u]!=)
dfs(Left[u]);
//printf("%c",u+'A'-1);//中序
if(Right[u]!=)
dfs(Right[u]);
printf("%c",u+'A'-);//后序
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%s%s",Per,In)!=EOF)
{
memset(Left,,sizeof(Left));
memset(Right,,sizeof(Right));
int n=strlen(Per);
rebuild(,n-,,n-);
dfs(Per[]-'A'+);
printf("\n");
}
return ;
}
UVa 536 Tree Recovery(二叉树后序遍历)的更多相关文章
- 二叉树后序遍历的非递归算法(C语言)
首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...
- POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)
1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)
传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...
- lintcode.68 二叉树后序遍历
二叉树的后序遍历 描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的后序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返 ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- 剑指Offer的学习笔记(C#篇)-- 平衡二叉树(二叉树后序遍历递归详解版)
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 一 . 题目分析 首先要理解一个概念:什么是平衡二叉树,如果某二叉树中任意的左右子树深度相差不超过1,那么他就是一颗平衡二叉树.如下图: 所以 ...
- 数据结构实验之求二叉树后序遍历和层次遍历(SDUT 2137)
Problem Description 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历. Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据. ...
随机推荐
- APP-2-Hbuilder开发环境搭建
1.Hbuilder下载 http://www.dcloud.io/hbuilderx.html 2.夜神模拟器下载 https://www.yeshen.com/ 3.chrome浏览器下载 htt ...
- ORM 的基本操作
https://www.cnblogs.com/sss4/p/7070942.html
- 尚硅谷redis学习9-发布订阅
是什么? 图示说明 命令 例子
- spark1.6.1 on yarn搭建部署
注:本文是建立在hadoop已经搭建完成的基础上进行的. Apache Spark是一个分布式计算框架,旨在简化运行于计算机集群上的并行程序的编写.该框架对资源调度,任务的提交.执行和跟踪,节点间的通 ...
- Oracle数据库备份/导入工具
expdp和impdp常用于ORACLE数据库的导入导出. expdp导出数据库 1.root用户创建用于impdp/expdp导入导出的目录: # mkdir -p /home/dmpdata # ...
- Hydra密码破译工具
Hydra简介 Hydra是著名黑客组织thc开发的一款开源的暴力密码破解工具,可以在线破解多种密码,目前已经被Backtrack和kali等渗透平台收录.除了命令行下的Hydra外,还提供了Hydr ...
- js 监听浏览器刷新还是关闭事件 - 转
监听页面关闭: window.onbeforeunload = function() { //鼠标相对于用户屏幕的水平位置 - 窗口左上角相对于屏幕左上角的水平位置 = 鼠标在当前窗口上的水平位置 v ...
- 一秒去除Win7快捷方式箭头
我相信有无数的小盆友跟我一样很讨厌Win7快捷方式图标上的箭头,实在太丑陋了,尤其是带有强迫症滴.现在介绍去除箭头的方式. 1. 打开编辑器,将以下代码粘贴进去,然后保存为.bat后缀的文件,然后双击 ...
- python 如何注释
一.单行注释 单行注释以#开头,例如: print 6 #输出6 二.多行注释 (Python的注释只有针对于单行的注释(用#),这是一种变通的方法) 多行注释用三引号' ...
- mysql 拼接字符
Mysql的查询结果行字段拼接,可以用下面两个函数实现: 1. concat函数 mysql> select concat('1','2','3') from test ; +--------- ...