1043 Is It a Binary Search Tree (25 point(s))

2022年全球市场微波等离子体CVD反应器总体规模、主要生产商、主要地区、产品和应用细分研究报告 据GIR (Global Info Research)调研,按收入计,2021年全球微波等离子体CVD反应器收入大约 百万美元,预计2028年达到 百万美元,2022至2028期间,年复合增长率CAGR为 %。 阅读详情

1043 Is It a Binary Search Tree (25 point(s))

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then Ninteger keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line YES if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or NO if not. Then if the answer is YES, print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

7
8 6 5 7 10 8 11

Sample Output 1:

YES
5 7 6 8 11 10 8

Sample Input 2:

7
8 10 11 8 6 7 5

Sample Output 2:

YES
11 8 10 7 5 6 8

Sample Input 3:

7
8 6 8 5 10 9 11

Sample Output 3:

NO

题意:

给出BST的前序遍历和BST及其镜像树的定义,判断其是否合法,若合法输出其后序遍历。

考点:

1. 二叉搜索树的定义和建立;

2. 树的前序遍历、后续遍历;

3. 根据二叉搜索树的递归定义,左子树的结点值都小于根结点,右子树的结点值都大于等于根结点(镜像树相反)可知,仅通过其前序遍历(根--左子树--右子树),就可以唯一确定树的结构,按照顺序插入元素即可。

思路:

基于3可知:

1. 首先判断树是BST还是其镜像树,假设其是给出的先序遍历是正确的,按顺序插入元素建树;

2. 对所建立的BST进行前序遍历,并和给出的比较;

3. 若相同,则假设成立,再对树进行后序遍历,否则假设错误。

注意点和值得学习的点:

通过引用把遍历结果记录再vector中(常规思路,但是有时候不容易想到);

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
struct Node{
	int data;Node* left;Node* right;
	Node(int d):data(d),left(NULL),right(NULL){ }
};
bool mirror = false;
void insert(Node* &root,int data){//注意是引用 
	if(root == NULL){//根节点是空,建立新节点 
		root = new Node(data);
		return;
	}
	if(mirror==false){//是二叉搜索树 
		if(data < root->data) insert(root->left,data);
		else insert(root->right,data); 
	} 
	else{//是二叉搜索树镜像树 
		if(data >= root->data) insert(root->left,data);
		else insert(root->right,data);
	}
}
void preOrder(Node* root,vector<int>& order){//二叉树的先序遍历 
	order.push_back(root->data);
	if(root->left!=NULL) preOrder(root->left,order);
	if(root->right!=NULL) preOrder(root->right,order);
} 
void postOrder(Node* root,vector<int>& order){//二叉树的后序遍历 
	if(root->left!=NULL) postOrder(root->left,order);
	if(root->right!=NULL) postOrder(root->right,order);
	order.push_back(root->data); 
} 
int main(void){
	int N,a;cin>>N;
	vector<int> preGot,preGiven,post;
	Node* root = NULL;
	for(int i=0;i<N;i++){
		cin>>a;
		preGiven.push_back(a);
	}
	if(preGiven.size()>1&&preGiven[1]>preGiven[0]) mirror = true;
	for(int i=0;i<N;i++){
		insert(root,preGiven[i]);
	}
	preOrder(root,preGot);
	if(preGot==preGiven){//如果根据给出的序列生成的二叉搜索树和实际建树遍历的一致,则说明输入合法 
		postOrder(root,post);
		cout<<"YES"<<endl;
		for(int i=0;i<post.size();i++){
			if(i==0) cout<<post[i];
			else cout<<" "<<post[i];
		}
	}
	else cout<<"NO"<<endl;
	return 0;
}

 

写一个b站爬虫(思路) part 0 安装 scrapy, requests part 1 信息获取 打开番剧索引链接(https://www.bilibili.com/anime/index/) F12控制台,选中NETWORK,来到xhr文件,我们可以找到了存放番剧json的地方 阅读详情

相关推荐

DeepBSA实战指南:从安装到基因组分析的全流程解析

本文详细解析了DeepBSA工具从安装到基因组分析的全流程,特别介绍了其简化生物信息学分析的'一键式'操作和独家深度学习算法。通过实战案例展示了DeepBSA在信噪比处理和QTL定位上的优势,并提供了Linux和Windows系统的安装避坑指南、数据预处理技巧以及算法选择建议,帮助用户高效完成基因组分析。

weixin_28732953的博客 443

1043 Is It a Binary Search Tree (25 point(s))

题解 BST遍历。 #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;vector&gt; using namespace std; vector&lt;int&gt; pre, post; bool isMirror; int n; void getpost(int l, int r) { if(l &gt; r)...

徐伯莱 352

大疆机场及无人机上云(航线规划、指令飞行...)

最近基于大疆上云api进行二次开发,后面将按照开发步骤对其进行说明!

资深三维GIS开发 5579

1043 Is It a Binary Search Tree (25)(25 point(s))

1043 Is It a Binary Search Tree (25)(25 point(s)) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only node...

Cute_jinx的博客 397

知识图谱本体建模之RDF、RDFS、OWL详解

(一)知识图谱本体建模之RDF、RDFS、OWL详解 1.语义网体系 知识图谱于2012年由Google提出,并不是新概念,而是由语义网络(Semantic Network)衍生而来。语义网络由相互连接的节点和边组成,节点表示概念或者对象,边表示他们之间的关系。RDF,RDFS与OWL都是基于语义网的知识表示框架,在节点和边的取值上做了约束,制定了统一标准,为多源数据的融合提供了便利。RDF,RDFS/OWL 属于语义网技术栈,它们的提出,使得语义网克服了语义网络的缺点。其语义网技术栈如下所示。 2.RD

周周的专栏 1万+

openclaw通俗入门系列6——记忆系统

记忆 = 智能体的笔记本,分长期(MEMORY.md)和每日(memory/日期.md)。只有写到文件里的才能记住!支持语义搜索(理解意思)+ 关键词搜索,还能自动在压缩前保存重要内容。

m0_60605453的博客 403

PAT A1043 Is It a Binary Search Tree (25point(s))

思路:第一步判断该先序遍历序列是否能构建查找二叉树或镜像查找二叉树。第二步,根据第一步得到的结果构建查找二叉树或镜像查找二叉树。第三步,对构建的树进行后序遍历。 注意点: 1.判断是否能构成查找二叉树的方法主要是将题给的先序遍历序列分成三个部分,首元素(根节点),一个是比首元素小的区间(左子树),一个是比首元素大的区间(右子树),从首元素+1的位置开始查找,找到第一个大于等于首元素的数(说明左子树...

皮皮瓜的博客 185

YOLOv13优化:PoolingFormerCGLU模块 MetaFormer 框架 + 池化混合器 手把手教你自注意力

PoolingFormer1 是一个基于 MetaFormer 框架 的轻量化卷积-Transformer 混合模块,通过 Pooling 取代自注意力,实现: 更低的计算成本 一定的全局信息融合 兼容 CNN 和 Transformer 的优点

FJN110的博客 426

PAT(甲级)1043 Is It a Binary Search Tree (25point(s))

题目 题目链接 思路 题目大意:给一些数字,将这些按顺序插入到二叉搜索树中,并判断二叉搜索树或镜像树的先序是否和给定序列相同,若相同,则输出原树或者 镜像树的后序; 首先是建立二叉搜索树,利用递归来建,递归边界是root == NULL,代表找到插入位置;注意二叉搜索树是不平衡的,树的形态与元素插入的顺序有关; 建好树之后就可以遍历原树的先序,并判断是否相等;若不相等,再去遍历镜像树的先序,判断是否相等; 遍历镜像树的先序就是先访问右子树再访问左子树; 代码 #include <iostream&gt

hjyp的博客 196

Mamba-YOLOv8深度解析:基于状态空间模型的下一代目标检测架构(含完整代码与实战部署)文末含资料链接!

通过本文的完整指南,您将掌握从理论创新到工程落地的全流程技术细节。立即动手实践,开启目标检测的新纪元!

FJN110的博客 519

“华为杯”研究生数学建模竞赛2019年-【华为杯】D题:汽车行驶工况构建(附获奖论文和MATLAB代码实现)

映了汽车道路行驶的运动学特征。准差、加速度标准差这九个特征参数用于后续的数据分析,建立了合理的汽车运动特征评。特征参数与所属类特征参数的特征值的相关性大小替代误差进行排序的,计算得到汽车行。对于问题一,我们首先对原始数据进行了时间变量转换,将原本的年月日时分秒的计。紧接着进行了运动学片段的划分,引入九个特征参数,通过主成分分析、本文研究的是汽车行驶工况构建,汽车行驶工况是描述汽车行驶的速度。的合理性评价标准,对两种方法都进行了合理性评价,得到的。进行了合理性评价,得到了最优的汽车行驶工况曲线。

getusushu的博客 1004

1043 Is It a Binary Search Tree (25 分)(算法笔记)

1043 Is It a Binary Search Tree (25 分) … Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N integer keys are given...

ostrich的博客 946

手把手教你学simulink实例--基于Simulink的船舶动力装置性能评估与仿真

船舶动力装置是船舶运行的核心系统,其性能直接影响船舶的安全性、经济性和环保性。基于MATLAB/Simulink平台进行船舶动力装置性能评估与仿真是研究和优化船舶动力系统的重要手段。通过建模与仿真,可以分析动力装置在不同工况下的性能表现,并验证控制策略的有效性。本项目旨在构建一个完整的船舶动力装置性能评估与仿真模型,包括发动机建模、传动系统建模、负载分析以及性能评估模块,并通过仿真分析其动态响应和性能指标。

MHD0815的博客 974

1043 Is It a Binary Search Tree(BST)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right ...

无名小白的博客 318

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

1043. Is It a Binary Search Tree (25)-PAT甲级真题

1043.Is It a Binary Search Tree (25) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with k

邪恶牛犊的博客 396

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

PAT 甲级 1043  Is It a Binary Search Tree

1043 Is It a Binary Search Tree (25 point(s)) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes wi...

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

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

PAT A1043 Is It a Binary Search Tree (25 分)

1043Is It a Binary Search Tree(25分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys...

树叶子的博客 467

1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys ...

Mq_Go的博客 849

1043 Is It a Binary Search Tree (25分)

1 题目 1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with ke...

韩江雪de 小屋 566

精解1043. Is It a Binary Search Tree (25)-PAT甲级真题

1043. Is It a Binary Search Tree (25)-PAT甲级真题 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes wit...

honeyShi's Blog 890

1043 Is It a Binary Search Tree

1043 Is It a Binary Search Tree 题意 题目给出二叉树的前序序列判断是否是BST 是输出YES 并且输出改BST的后序序列 思路 ​ 通过前序和中序 来 求后序。 ​ 由于是BST 所有数据的中序一定是有序的。题目说给的前序序列可能是 左右交换过的BST 所以需要先判断一下,如果是镜像的 BST 那么他的前序的第二个一定是大于根节点也就是第一个。 if(CNT&gt;...

sologala的博客 365

1043 Is It a Binary Search Tree (25 分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right subt...

weixin_42582136的博客 796

1043 Is It a Binary Search Tree(25 分)

1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys ...

chenyutingdaima的博客 550

PAT 1043 Is It a Binary Search Tree

原题链接:1043 Is It a Binary Search Tree (25分) 关键词:BST 题目大意: 给出一些整数序列,问你这有没有可能是二叉排序树或者二叉排序树镜像的前序遍历,如果是的话还要输出后序遍历。

cwtnice的博客 170
上一篇: 1022 Digital Library (30 point(s))
下一篇: 1016 Phone Bills (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值