首页 技术 正文
技术 2022年11月21日
0 收藏 670 点赞 2,580 浏览 2472 个字

Source:

PAT A1119 Pre- and Post-order Traversals (30 分)

Description:

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

Keys:

Attention:

  • 如果树中所有的分支结点都有两个孩子,那么由先序和后序可以唯一确定一棵二叉树;

Code:

 /*
Data: 2019-06-29 17:43:37
Problem: PAT_A1119 Pre- and Post-order Traversals
AC: 36:40 题目大意:
给出先序和后序遍历,输出任意中序遍历,并判断是否唯一 基本思路:
即由先序和后序是否可以唯一的构造一棵二叉树?
易知先序+中序,后序+中序,层序+中序,可以唯一的构造一棵二叉树,
为什么呢?因为已知根结点和中序遍历,能够求出根结点的左子树和右子树,进而递归的构造一棵二叉树
重点在于找到根结点的左子树和右子树,
先序遍历中,根结点root的下一个结点设为左子树的根结点lchild
后序遍历中,找到lchild,那么lchild之前(含root)的结点均为左子树,lchild之后直至root之前的结点均为右子树
这样,我们可以获得一棵二叉树;
如何判定是否唯一呢?
若分支结点既有左子树,又有右子树,那么我们可以通过上述方法唯一的构造一棵二叉树
若分支结点只有左子树或右子树
则我们既可以认为它是左子树,又可以认为它是右子树,这样就产生了多个解, 即此时二叉树不唯一
当我们总假设该子树为左子树,这样可以就获得其中一棵二叉树了
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=;
int pre[M],post[M],ans=;
vector<int> in; void Travel(int preL, int preR, int postL, int postR)
{
if(preL > preR){
ans=;
return;
}
if(preL == preR){
in.push_back(pre[preL]);
return;
}
int k;
for(k=postL; k<=postR; k++)
if(pre[preL+] == post[k])
break;
int numLeft = k-postL;
Travel(preL+,preL++numLeft,postL,k);
in.push_back(pre[preL]);
Travel(preL++numLeft+,preR,k+,postR-);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &pre[i]);
for(int i=; i<n; i++)
scanf("%d", &post[i]);
Travel(,n-,,n-);
if(ans) printf("Yes\n");
else printf("No\n");
for(int i=; i<n; i++)
printf("%d%c", in[i],i==n-?'\n':' '); return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,909
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,434
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,249
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,060
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,692
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,730