PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]
1119 Pre- and Post-order Traversals (30 分)
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input 1:
7
1 2 3 4 6 7 5
2 6 7 4 5 3 1
Sample Output 1:
Yes
2 1 6 4 7 3 5
Sample Input 2:
4
1 2 3 4
2 4 3 1
Sample Output 2:
No
2 1 3 4
题目大意:假设一棵二叉树的所有关键字均是不同的正整数。给出前序遍历和后序遍历,如果中序是唯一的,那么就输出Yes,并且输出中序遍历;如果不唯一,那么就输出No,并且输出其中一种遍历序列即可。
//啊啊,刚看到这个题就很懵,因为以前只知道前序和后序不能确定一棵二叉树,但是也没往深里想。
//不太会,放弃。
主要是因为没有中根无法区分左右子树,对于给的样例来说:
1 2 3 4 和后序2 4 3 1 它的中根可能有:2 1 3 4和2 1 4 3 两种。4无法确定是3的左节点还是右节点。
代码转自:https://www.liuchuo.net/archives/2484
#include <iostream>
#include <vector>
using namespace std;
vector<int> in, pre, post;
bool unique = true;
void getIn(int preLeft, int preRight, int postLeft, int postRight) {
if(preLeft == preRight) {
in.push_back(pre[preLeft]);
return;
}
if (pre[preLeft] == post[postRight]) {//如果找到了根的话。
int i = preLeft + ;
//在前序遍历种找到下一个根节点。
while (i <= preRight && pre[i] != post[postRight-]) i++;
if (i - preLeft > )//如果相当于一个子树为空?它下一个遍历又是根节点?
getIn(preLeft + , i - , postLeft, postLeft + (i - preLeft - ) - );
//最后一个元素是加上当前要去遍历的部分的长度。
else
unique = false;
in.push_back(post[postRight]);//把根push了进去。
getIn(i, preRight, postLeft + (i - preLeft - ), postRight - );
//一次处理之后就把根和左子树
}
}
int main() {
int n;
scanf("%d", &n);
pre.resize(n), post.resize(n);
for (int i = ; i < n; i++)
scanf("%d", &pre[i]);
for (int i = ; i < n; i++)
scanf("%d", &post[i]);
getIn(, n-, , n-);
printf("%s\n%d", unique == true ? "Yes" : "No", in[]);
for (int i = ; i < in.size(); i++)
printf(" %d", in[i]);
printf("\n");
return ;
}
//好难的这道题,不太会,还是需要复习。
PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]的更多相关文章
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 数据结构作业——order(二叉树遍历)
order Description 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后 序遍历. Input 输入第一行为一个正整数 n 表示二叉树的节点数目, 节点编号从 1 到 n ...
- 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 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- C++ 二叉树遍历实现
原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...
- poj2255 (二叉树遍历)
poj2255 二叉树遍历 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descripti ...
随机推荐
- linux学习笔记28--监控命令vmstat,iostat, lsof
linux的监控包括多个方面,常用的是进程,内存,I/O,磁盘空间这三个方面. vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程. ...
- (译)Getting Started——1.1.2 Basic(基础)
本节教程会带你浏览创建简单的用户界面.添加自定义行为的整个过程.完成本节教程后,你创建的应用就可以运行在iPhone和iPad上了. 本节教程会教给你如下技能: 1. 使用Xcode创建和管理项目 2 ...
- Macbook上Windows的触摸板设置工具
Macbook上用Boot Camp装了双系统后,没了触摸板的三指拖拽功能,滚动(scroll)也太过灵敏,装Boot Camp官方驱动也没用. 装了Trackpad++这个第三方驱动,就能完美实现M ...
- CentOS 7 ifconfig: command not found
# ifcon-bash: ifconfig: command not found谷歌了一下,整理了一下解决思路 查看ifconfig命令是否存在 查看 /sbin/ifconfig是否存在 如果if ...
- Hive学习笔记——基本配置及测试
1.什么是Hive Hive 是建立在 Hadoop上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在Hadoop中的大规模数据的机 ...
- oracle sqlplus 常用命令大全
show和set命令是两条用于维护SQL*Plus系统变量的命令 SQL> show all --查看所有68个系统变量值 SQL> show user --显示当前连接用户 SQL> ...
- 判断下列语句是否正确,如果有错误,请指出错误所在?interface A{
判断下列语句是否正确,如果有错误,请指出错误所在? interface A{ int add(final A a); } class B implements A{ long add(final A ...
- 一个事半功倍的c#方法 动态注册按钮事件
前几天在网上看见一个制作计算器的c#程序,其中有一个动态注册按钮事件,觉的很有用.于是实际操作了一哈, 确实比较好. 言归正传,下面就来讲讲怎样动态注册按钮事件. 首先,我们需要设置变量来获取点击一个 ...
- ChemDraw绘制苯甲酸钠的生成反应式的方法
苯甲酸钠是苯甲酸的钠盐,就用途来说苯甲酸主要用于制作杀真菌剂,苯甲酸钠则是用于食物或软饮料中的主要商品防腐剂,其防腐最佳PH是2.5-4.0,苯甲酸钠的亲油性也很大,易穿透细胞膜进入细胞体内.苯甲酸钠 ...
- js 数组求和,多种方法,并比较性能
可以借用下面12种方法对数组求和,创建一个长度为10w的数组,进行测试 every() 检测数值元素的每个元素是否都符合条件. filter() 检测数值元素,并返回符合条件所有 ...