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

分析:根据序列建树,关键还是左右子树的划分,先序序列为N L R,后序为L R N,可以首先确定下根节点,后序中的R又可以分为L R N,因此后序的倒数第二个就是右子树的根节点,在先序序列中寻找该节点的位置就能确定左右子树,当\(pos = preL + 1\)时,就会出现不唯一的情况

#include<iostream>
#include<cstdio>
#include<vector>
#include<unordered_map>
#include<string>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std;
const int nmax = 40;
int pre[nmax], post[nmax];
struct node{
int v;
node *lchild, *rchild;
};
typedef node* pnode;
bool uq = true;
pnode creat(int preL, int preR, int postL, int postR){
if(preL > preR)return NULL;
if(preL == preR){
pnode root = new node;
root->v = pre[preL];
root->lchild = root->rchild = NULL;
return root;
}//这是为了防止在寻找pos时发生数组越界
pnode root = new node;
root->v = pre[preL];
root->lchild = root->rchild = NULL;
int pos = preL + 1;
while(pre[pos] != post[postR - 1])pos++;
if(pos == preL + 1)uq = false;
int numleft = pos - preL - 1;
root->lchild = creat(preL + 1, pos - 1, postL, postL + numleft - 1);
root->rchild = creat(pos, preR, postL + numleft, postR - 1);
return root;
}
vector<int>ans;
void inOrder(pnode root){
if(root == NULL)return;
inOrder(root->lchild);
ans.push_back(root->v);
inOrder(root->rchild);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i)scanf("%d", &pre[i]);
for(int i = 0; i < n; ++i)scanf("%d", &post[i]);
pnode root = creat(0, n - 1, 0, n - 1);
if(uq == true)printf("Yes\n");
else printf("No\n");
inOrder(root);
for(int i = 0; i < ans.size(); ++i){
if(i > 0)cout<<" ";
cout<<ans[i];
}
cout<<endl;//要输出这个换行符,否则会显示格式错误
return 0;
}

【刷题-PAT】A1119 Pre- and Post-order Traversals (30 分)的更多相关文章

  1. 【刷题-PAT】A1095 Cars on Campus (30 分)

    1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...

  2. 【PAT甲级】1119 Pre- and Post-order Traversals (30分)(已知先序后序输出是否二叉树唯一并输出中序遍历)

    题意: 输入一个正整数N(<=30),接着输入两行N个正整数第一行为先序遍历,第二行为后续遍历.输出是否可以构造一棵唯一的二叉树并输出其中一颗二叉树的中序遍历. trick: 输出完毕中序遍历后 ...

  3. 【刷题-PAT】A1135 Is It A Red-Black Tree (30 分)

    1135 Is It A Red-Black Tree (30 分) There is a kind of balanced binary search tree named red-black tr ...

  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 L3-008. 喊山(BFS)C4 初赛30分

    喊山(30 分) 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的. ...

  6. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  7. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  8. PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

    1034 Head of a Gang (30 分)   One way that the police finds the head of a gang is to check people's p ...

  9. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

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

随机推荐

  1. CF475A Bayan Bus 题解

    Update \(\texttt{2020.10.6}\) 修改了一些笔误. Content 模拟一个核载 \(34\) 人的巴士上有 \(k\) 个人时的巴士的状态. 每个人都会优先选择有空位的最后 ...

  2. python 字符编码讲解

    ANSI不是一种具体的编码格式 ANSI在中文Windows操作系统代码指的是GBK编码 ANSI在中文Mac操作系统代码指的是UTF-8编码 ANSI在其他国家的操作系统中有其他的编码格式 #ASC ...

  3. Java面向对象~类和对象&方法,类方法

    面向对象 概念:     1.同一类事物的抽象描述,不是具体的    2.类和对象的关系:        类 是抽象的.        对象 是具体的.    3.对象的体征,称为"属性&q ...

  4. Python3 中bytes数据类型深入理解(ASCII码对照表)

    bytes的来源 bytes 是 Python 3.x 新增的类型,在 Python 2.x 中是不存在的. bytes 的意思是"字节",以字节为单位存储数据.而一个字节二进制为 ...

  5. ligerUI问题

    1.checkboxColWidth:990,Grid的复选框的宽度设置为什么不起作用. 2.当grid出现横线不对齐时,可以设置detailColWidth:90,属性进行设置.此属性好像只是针对复 ...

  6. 【九度OJ】题目1199:找位置 解题报告

    [九度OJ]题目1199:找位置 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1199 题目描述: 对给定的一个字符串,找出有重复的 ...

  7. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  8. Unknown Treasure(hdu5446)

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  9. Codeforces 777C:Alyona and Spreadsheet(预处理)

    During the lesson small girl Alyona works with one famous spreadsheet computer program and learns ho ...

  10. libecc:一个可移植的椭圆曲线密码学库

    libecc:一个可移植的椭圆曲线密码学库 这段时间要写毕设关于椭圆曲线的部分,就参考了一个椭圆曲线库的代码来编写.这个库中的代码的结构.风格和封装在我看来是十分完善的.用起来也比较方便,当作一个密码 ...