PAT1119. Pre- and Post-order Traversals
思路:中序遍历–根结点,左子树,右子树;后序遍历–左子树,右子树,根结点。
那么在找到根结点之后就可以开始划分左右子树了。左子树的先序第一个节点是根,左子树的后序最后一个节点是根。
例如
1 2 3 4 6 7 5
2 6 7 4 5 3 1
当前的根结点就是1,在先序中得到左子树的根结点就是2,再在后序中知道左子树的节点就是{2},那么就得到左子树了,剩下的就是右子树。
如何判断重建的树是否唯一?非叶结点如果只有左子树或者右子树,那么就不唯一。
注意:如果你使用数组实现的二叉树,那么把数组开大一点,只开maxn=30+5的话第一组数据会WA(本人WA了半个小时才猜到这个套路),后来直接开10000过掉。
AC代码
#include <stdio.h>
#include <string.h>
const int maxn = 10000+5;
bool isUnique;
int pre[maxn], post[maxn];
int left[maxn], right[maxn];
int ans[maxn], nodes;
//return root
int reBuild(int prel, int prer, int postl, int postr) {
if(prel > prer) return -1;
int root = pre[prel];
if(prel == prer) { //leaf
return root;
}
//left-tree
int l1 = prel+1, r2;
for(r2 = postl; r2 < postr; r2++) {
if(post[r2] == pre[l1]) {
break;
}
}
int len = r2 - postl + 1;
// check unique
if(len == prer-prel) {
isUnique = false;
}
int r1 = l1 + len -1;
int l2 = postl;
//rebulid left-tree
left[root] = reBuild(l1, r1, l2, r2);
//rebuild right-tree
right[root] = reBuild(r1+1, prer, r2+1, prer-1);
return root;
}
void getInorder(int root) {
if(root == -1) return;
getInorder(left[root]);
ans[nodes++] = root;
getInorder(right[root]);
}
int main() {
int n;
while(scanf("%d", &n) == 1) {
isUnique = true;
memset(left, -1, sizeof(left));
memset(right, -1, sizeof(right));
for(int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
}
for(int i = 0; i < n; i++) {
scanf("%d", &post[i]);
}
int root = reBuild(0, n-1, 0, n-1);
nodes = 0;
getInorder(root);
if(isUnique) {
printf("Yes\n");
} else {
printf("No\n");
}
for(int i = 0; i < nodes; i++) {
printf("%d%c", ans[i], i == nodes-1 ? '\n' : ' ');
}
}
return 0;
}
如有不当之处欢迎指出!
PAT1119. Pre- and Post-order Traversals的更多相关文章
- Construct a tree from Inorder and Level order traversals
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...
- [LeetCode] Rank Scores 分数排行
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- ASP.NET MVC : Action过滤器(Filtering)
http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...
- HDU 1160 FatMouse's Speed
半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- Spring Cloud Zuul 限流详解(附源码)(转)
在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal
Pre: node 先, Inorder: node in, Postorder: node 最后 PreOrder Inorde ...
随机推荐
- Servlet和web服务器关系
前面的博客我详细的罗列了下Servlet的常用的类和接口,然后在前面的前面我类似tomcat模拟了一套web服务器,这里来做一个统一的整理,这样子可以更好的把握Servlet,也可以更好的了解下web ...
- Java常用类--数字常用类
math java提供了基本的 + - * / %等基本算术运算的运算符,但对于更复杂的数学运算比如:三角函数,对数运算,指数运算就无能为力了.Java提供了Math工具类来完成这些复杂的运算,Mat ...
- 自己用的一套reset.css,打算整理一下方便以后用,持续更新中,各位大神,不喜勿喷
*{margin: 0; padding: 0;border:none;}img{vertical-align: top;width: 100%;border: none;}ul,li{list-st ...
- CentOS修改Tomcat端口号
Linux下修改Tomcat默认端口 1.方法一 假设tomcat所在目录为/usr/local/apache-tomcat/ 1.打开tomcat配置文件 # vi /usr/local/apach ...
- 【转】Linux Oracle服务启动&停止脚本与开机自启动
在CentOS 6.3下安装完Oracle 10g R2,重开机之后,你会发现Oracle没有自行启动,这是正常的,因为在Linux下安装Oracle的确不会自行启动,必须要自行设置相关参数,首先先介 ...
- django xadmin 集成DjangoUeditor富文本编辑器
本文档记录自己的学习历程! 介绍 Ueditor HTML编辑器是百度开源的在线HTML编辑器,功能非常强大 额外功能 解决图片视频等无法上传显示问题 Ueditor下载地址 https://gith ...
- 自定义Func模块
自定义Func模块 (1)自定义模块步骤 (2)生成模块 [root@controller modules]# cd /usr/lib/python2.7/site-packages/func/min ...
- linux 搭建PPTP
pptp简介 PPTP,Point to Point Tunneling Protocol,点对点隧道协议,这是一种支持多协议虚拟专用网络(VPN)技术.远程用户能够通过装有点对点协议的系统安全访问公 ...
- Java使用Openoffice将word、ppt转换为PDF
最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览. OpenOffice OpenOffice.org 是一套跨平台的办公室软件套件,能在 W ...
- 使用mybatis从mysql里进行模糊查询的编码问题
关于这个问题,记录下我的解决方法,希望对有同样困惑的朋友,有所帮助. 问题描述: 我在做mybatis从mysql里模糊查询时,如果模糊的关键词是字母的话,可以查出来.如果模糊的关键词是汉字的话,查不 ...