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
 #include <iostream>
#include <vector>
using namespace std;
int n, a;
vector<int>preOrder, postOrder, inOrder;
bool flag = true;//表示树的形态不唯一
void getInOrder(int root, int left, int right)
{
if (left >= right)
{
if (left == right)//只有一个节点
inOrder.push_back(preOrder[root]);
return;
}
int i = left;
while (i < right && preOrder[root + ] != postOrder[i])//查找前序遍历中下一个节点在后序中的位置
++i;
if (i == right - )//先根序列中根节点的下一结点在后根序列中的位置正好等于right-1
flag = false;
getInOrder(root + , left, i);
inOrder.push_back(preOrder[root]);
getInOrder(root + i - left + , i + , right - );
}
int main()
{
cin >> n;
for (int i = ; i < n; ++i)
{
cin >> a;
preOrder.push_back(a);
}
for (int i = ; i < n; ++i)
{
cin >> a;
postOrder.push_back(a);
}
getInOrder(, , n - );
cout << (flag ? "Yes" : "No") << endl;
for (int i = ; i < n; ++i)
cout << (i > ? " " : "") << inOrder[i];
cout << endl;
return ;
}

PAT甲级——A1119 Pre- and Post-order Traversals【30】的更多相关文章

  1. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  2. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  3. PAT 甲级1135. Is It A Red-Black Tree (30)

    链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is ...

  4. PAT甲级——A1127 ZigZagging on a Tree【30】

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  5. PAT甲题题解-1119. Pre- and Post-order Traversals (30)-(根据前序、后序求中序)

    (先说一句,题目还不错,很值得动手思考并且去实现.) 题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可. 已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在 ...

  6. pat 甲级 1135. Is It A Red-Black Tree (30)

    1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  7. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

  8. 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)

    题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...

  9. 【PAT甲级】1038 Recover the Smallest Number (30 分)

    题意: 输入一个正整数N(<=10000),接下来输入N个字符串,每个字符串包括至多8个字符,均为数字0~9.输出由这些字符串连接而成的最小数字(不输出前导零). trick: 数据点0只包含没 ...

随机推荐

  1. Servlet(Server Applet) 详解

    Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. Servlet的工作模式 客户端发送请求至服务器 服务器启动并调用Servlet,Servlet根据客户端请求生 ...

  2. 【磁盘】顺序IO比随机IO快

    假设磁盘每秒可以做100个随机I/O操作,并且可以完成每秒10MB的顺序读取(这大概是消费级磁盘现在能达到的水平).如果每行100字节,随机读每秒可以读100行(相当于每秒10000字节=10KB), ...

  3. NX二次开发-UFUN获取点在面上的向量方向UF_MODL_ask_face_props【转载】

    1 NX11+VS2013 2 3 4 #include <uf.h> 5 #include <uf_ui.h> 6 #include <uf_modl.h> 7 ...

  4. python入门 集合(四)

    集合 集合是一个无序的不重复元素序列,可以迭代,也可以修改.集合迭代的时候元素是随机的. 集合通常用来 membership testing, 去重, 也可以用来求交集并集补集. 介绍一下如何创建集合 ...

  5. 1.RabbitMQ介绍

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...

  6. ZK4字命令

    zookeeper4字命令:两种方式,1.通过telnet链接服务器,执行stat.2.echo stat|nc xxx.xxx.xxx.xxx 2181效果是一样的conf:zk服务器运行时的基本信 ...

  7. NetBeans简介和简单使用

    1.什么是NetBeans? NetBeans IDE:可以使开发人员利用Java平台能够快速创建Web.企业.桌面以及移动的应用程序: 支持语言:PHP.Ruby.JavaScript.Groovy ...

  8. BOM DOM 简介

    BOM和DOM简介 BOM,Browser Object Model ,浏览器对象模型. BOM主要提供了访问和操作浏览器各组件的方式. 浏览器组件:   window(浏览器窗口)   locati ...

  9. 牛客练习赛43D Tachibana Kanade Loves Sequence

    题目链接:https://ac.nowcoder.com/acm/contest/548/D 题目大意 略 分析 贪心,首先小于等于 1 的数肯定不会被选到,因为选择一个数的代价是 1,必须选择大于1 ...

  10. 20140415 HOG 不同继承方式的访问特性 虚函数工作原理

    1.HOG block重叠的好处 由于行人通常其形状可以视为柔体,人 的边缘位置不固定,而有一些移动,block 重叠后,一个边缘的梯度信息在两个相邻重叠的 block 中都能有所表达,这样即使边缘的 ...