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 [二叉树遍历][难]的更多相关文章

  1. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  3. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. 数据结构作业——order(二叉树遍历)

    order Description 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后 序遍历. Input 输入第一行为一个正整数 n 表示二叉树的节点数目, 节点编号从 1 到 n ...

  5. 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 ...

  6. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  7. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. C++ 二叉树遍历实现

    原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...

  9. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

随机推荐

  1. linux学习笔记34--命令rcp和scp

    rcp代表“remote file copy”(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝到另一个目录中. 1 ...

  2. linux学习笔记13--命令head和tail

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  3. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  4. VMWare虚拟机 网络连接模式

    这个是rocks 群里面我的一个朋友分享的,我觉很好而且描述的很清楚,这是一个做事的态度问题.   1 VMWare虚拟机bridged.host-only和NAT网络模式的区别和用法 VMWare提 ...

  5. 第一百五十一节,封装库--JavaScript,表单验证--密码确认验证--回答验证--电子邮件验证加自动补全

    封装库--JavaScript,表单验证--密码确认验证--回答验证--电子邮件验证加自动补全 效果图 html <div id="reg"> <h2 class ...

  6. jquery监听事件on写法以及简单的拖拽效果

    引子——关于jquery的某些写法 我先不对监听事件做解释,我们先来看下jquery的一些写法吧!我们最常用的是jquery的css()方法,相信大家都会用! 假如用css设置一个属性,我们写法如下: ...

  7. 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...

  8. 【Python】求素数-未经过任何优化

    print 'Find prime number smaller then input number \n' print 'Please input a number:' import time nu ...

  9. jquery动态调整div大小使其宽度始终为浏览器宽度

    需要设置宽度为整个浏览器宽度的div,当然我们可以使用相对布局的方式做到这一点,下面是具体实现,大家可以参考下 有时候我们需要设置宽度为整个浏览器宽度的div,当然我们可以使用相对布局的方式做到这一点 ...

  10. python+selenium之字符串切割操作

    python+selenium之字符串切割操作 在Python中自带的一个切割方法split(),这个方法不带参数,就默认按照空格去切割字段,如果带参数,就按照参数去切割. 新建一个python文件, ...