PAT 1119 Pre- and Post-order Traversals

TPAMI审稿人关注什么 写在开头:被tpami接收大概是每个cs er最开心的事情吧!今天阅读TPAMI的文章时,发现有扩刊的文章,也有不是扩刊的文章,那么这两类文章哪个更容易被接收呢? 阅读详情

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.
在这里插入图片描述
题解:
题目大意:给了树的前后序遍历,输出中序遍历,若遍历不唯一,输出No和其中一个答案即可。
可以根据任意一个序列确定根节点,但如何结合前后序列确定左右子树边界?如何判断这棵树不唯一呢?这是这个题的难点。
在这里插入图片描述
分析可以得到,在前序里,1是根节点,2是左子树根节点;在后序里,3是右子树根节点,**通过两个子树根节点的位置可以判断出左右子树的结点个数,**通过递归可以输出中序序列。
这个问题很容易解决,但是如何解决判断该子树是否唯一呢?
这里我其实不太清楚,但根据给的两个案例,我做了一个大胆的预测,若前序根节点的紧跟着的左子树根节点在后序遍历里还是右子树根节点的话,那就不唯一(当时其实没有想得这么清晰,通过后想想应该是这么回事)
代码:

#include<iostream>
#include<vector>
using namespace std;
int pre[39],post[39];
vector<int> in;
bool flag=true;
void inorder(int preL,int preR,int postL,int postR){
	if(preL>preR) return;
	int root=pre[preL]; //记下根节点的值
	int rroot=preL+1;
	while(rroot<=preR&&pre[rroot]!=post[postR-1]) rroot++; //找到右子树在前序序列的位置
	int numL=rroot-preL-1;
	if(preL+1<=preR&&postR-1>=postL&&pre[preL+1]==post[postR-1]) flag=false;
	inorder(preL+1,rroot-1,postL,postL+numL-1);
	in.push_back(root);
	inorder(rroot,preR,postL+numL,postR-1);
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>pre[i];
	for(int i=1;i<=n;i++) cin>>post[i];
	inorder(1,n,1,n);
	if(flag) printf("Yes\n");
	else printf("No\n");
	for(int i=0;i<in.size();i++) printf("%d%s",in[i],i==in.size()-1?"\n":" ");
	return 0;
}

在这里插入图片描述
其实在第一次提交的时候发现全部显示格式错误,我看了看测试区,好像就只在最后少了一个换行,调了一下后发现哦还真是这里的原因啊。

⭐️VMwareWorkstation17.0虚拟机安装搭建Windows 7虚拟机(完整图文详细步骤含配置VMware Tools教程) 本教程为大家提供了从安装VMware Workstation 17.0开始,到在虚拟机上成功搭建Windows 7专业版系统的完整步骤。我们详细介绍了准备阶段所需的软件和硬件要求,以及在VMware中配置和安装Windows 7系统的每一步操作。此外,我们还特别提到了如何在虚拟机上配置VMware Tools,以便更好地与宿主机进行交互。 通过本教程的学习,读者不仅能够掌握在VMware Workstation 17.0上安装Windows 7虚拟机的技能,还能了解到如何配置虚拟机的硬件以及安装VMwar 阅读详情

相关推荐

gesp c++ 八级知识点

gesp c++ 八级知识点

MMD.然大大 2237

1119 Pre- and Post-order Traversals

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

weixin_30788731的博客 133

RJ45以太网接口EMC设计方案

RJ45以太网接口EMC设计方案 一、 接口概述 RJ45以太网接口是目前应用最广泛的通讯设备接口,以太网口的电磁兼容性能关系到通讯设备的稳定运行。赛盛技术应用电磁兼容设计平台(EDP)软件从接口原理图、结构设计,线缆设计三个方面来设计以太网口的EMC设计方案。 二、 接口电路 原理图的EMC设计 百兆以太网接口2KV防雷滤波设计 图1 百兆以太网接口2KV防雷滤波设计 接口电路设计概述: 本方案从EMC原理上,进行了相关的抑制干扰和抗敏感度的设计;从设计层次解决EMC问题;同时此电路兼容了百兆以太网接

兰陵笑笑生SD的博客 1万+

PAT甲组1119 Pre- and Post-order Traversals 思路解析和代码

A1119 题目链接 个人思路 个人思路代码 #include <bits/stdc++.h> using namespace std; int N; int pre[105], post[105]; struct Node{ int data; int left, right; }nodes[105]; int pos; int newNode(int x) { node...

九筒的博客 409

SV fork join_none & for循环结合使用

fork join_none与循环联用常见问题解决方案

ZhangShuHuai11的博客 880

100个Python代码大全:学完这篇从Python小白到架构师

Python是一种广泛使用的高级编程语言,以其简洁优雅、易于学习和强大的库而闻名。无论你是一位经验丰富的程序员还是初学者,都可以从这种语言的丰富资源中受益。在本篇博客中,我将分享100个实用的Python代码示例,旨在帮助你掌握Python编程,并在日常工作中提高效率。

penggerhe的博客 1万+

【PAT】1119 Pre- and Post-order Traversals 思路加测试点讲解

思路讲解在我的另外一个博客中:【后序遍历】+【先序遍历】与【中序遍历】的关系 关于有几个测试点出错 AC代码: #include<bits/stdc++.h> using namespace std; const int MAXN = 31; int N,pre[MAXN],post[MAXN]; struct BiNode { int left, right; BiNode() { left = 0; right = 0; } }BiTree[100]; //l1 h1表示pre的上

目标:NLP算法工程师 470

Zabbix 简介、优缺点、基础原理

(8条消息) Zabbix 的基础概念、原理和架构_Lee木木的博客-优快云博客_zabbix架构类型Zabbix 的基础概念、原理和架构_Lee木木的博客-优快云博客_zabbix架构类型 --------------------------------------------------------------------------------------------------------------------------- 一、Zabbix 简介 Zabbix是由A...

weixin_50196615的博客 2037

1119 Pre- and Post-order Traversals (30 分)

题目大意:给定前序和后序遍布,来判断中序遍历是否唯一 因为当一个节点只有一个孩子的时候,不管是左孩子还是右孩子,他们的前序遍历都是一样的,后序遍历也是一样。 A A / \ B B \ / C C 这两个树的前序都是ABC后序都是CBA,所以我们无法确定左右孩子。 我们知道前序和后序不能唯一确定一颗树,前序是根左右,后序是左右根,我们发现前序遍历的第一个节点preL肯定是等于后序遍历的最后一个节点postR,preL+1是左孩子(当左孩子不存在,就是右孩子),postR-1是右孩子(同理),

qq_55591643的博客 576

在Redroid容器中构建Frida RPC服务:自动化生成淘宝X-Sign签名参数

本文详细介绍了在Redroid容器中构建Frida RPC服务,实现自动化生成淘宝X-Sign等签名参数的完整方案。通过逆向分析定位关键方法,定制Redroid系统使Frida-Server常驻运行,并编写Frida脚本将签名方法暴露为RPC接口,最终封装为可远程调用的微服务,解决了签名生成的可移植性与工程化难题。

ice55的博客 643

【系统分析师】2025年下半年真题:综合知识-答案及详解(回忆版)

本文摘要包含系统分析师考试相关题目解析,主要涵盖软件工程、数据库、网络协议和项目管理等知识点。内容包括:软件再工程与逆向工程的区别(D);GNU GPL开源协议特性(A);TCP/IP协议分层(A、B);数据库范式判定(A、B);前导图进度管理工具(C);系统规划中的数据类识别方法(B、D);关系代数除运算(C);CMM优化级特征(A);回归测试重要性(C)。这些解析帮助理解核心概念和考试重点。

数据知道的博客 6716

PAT甲级1119 Pre- and Post-order Traversals

PAT甲级1119 Pre- and Post-order Traversals 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 seque

weixin_44535077的博客 584

华为OD机试 - 直捣黄龙 - 广度优先搜索BFS(Python/JS/C/C++ 新系统 200分)

从样例 1 可知:5 / 2,2 -> 输出 2 9如果按“移动步数”计算应该是 8,但题目输出是 9,说明这里的“路径长度”统计的是 路径上经过的节点数量。最短路径条数:ways[end]路径长度:dist[end] + 1。

学Java,找哪吒 125

PAT甲级1119 Pre- and Post-order Traversals (30分):[C++题解]暴搜dfs、前序遍历和后序遍历求中序遍历

刷题

阿正的梦工坊 579

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

pat甲级1119 Pre- and Post-order Traversals

pat甲级1119 Pre- and Post-order Traversals note:是否有唯一二叉树,关键在于看非叶节点中是否存在只有一个子树的根节点,若有,则二叉树不唯一,因为无法确定该子树是左子树还是右子树。题目中说二叉树不唯一时,输出其中一种可能的中序序列即可,因此可以将唯一的子树看作左子树,也可以看作右子树,我的做法是看作左子树。 中序遍历方法:首先要确定根节点和子树结点。根节点是先序序列的队首数字也是后序序列的队尾数字。若根节点为非叶子节点,则左子树必然存在,且左子树根节点为先序序列中根节

qq_44992782的博客 206

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

1119 Pre- and Post-order Traversals——PAT甲级

【代码】1119 Pre- and Post-order Traversals——PAT甲级。

flyidj的博客 818

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

PAT Advanced 1119 Pre- and Post-order Traversals (30 )

PAT Advanced 1119 Pre- and Post-order Traversals (30 )题目描述Input Specification:Output Specification:Sample Input:Sample Output:解题思路Code 题目描述 Input Specification: Output Specification: Sample Input: ...

gq__97的博客 233

PAT_A1119 Pre- and Post-order Traversals

Source: PAT A1119Pre- 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 giv...

weixin_34356310的博客 137

PAT 甲级 1119  Pre- and Post-order Traversals

1119 Pre- and Post-order Traversals (30 point(s)) 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 in...

漫浸天空的雨色的备忘录~ 768

PAT甲1119 Pre- and Post-order Traversals

题意 给定先序,后序,判断是否能构造一棵唯一的二叉树 若能,则输出Yes及层序遍历 不能,则输出No及任意一解的层序遍历 2019南大计算机夏令营机试(外省)第三题 给定先序后序求能构造出多少种不同的二叉树 源码 #include<iostream> #include<vector> using namespace std; struct Node{ int val;...

Chauncey_Yu的博客 194

PAT甲级 1119 Pre- and Post-order Traversals

浙江大学PAT题目解答内容. Contribute to ZouJiu1/PAT development by creating an account on GitHub.GitHub - ZouJiu1/PAT: 浙江大学PAT题目解答内容。

邹九的个人博客 321

PAT A1119 Pre- and Post-order Traversals

PAT A1119 Pre- and Post-order Traversals 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 分析:先序后序建树:...

qq_42347617的博客 287
上一篇: leetcode 6. ZigZag Conversion
下一篇: leetcode 7. Reverse Integer
鲤鱼酒酒
博客等级 码龄7年 6粉丝 126原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值