【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意:
输入一个正整数N(<=1000),接下来输入N个点的序号。如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO。
trick:
for(auto it:post)
cout<<it<<((it!=post[n-1])?" ":"");
这样输出会使第0,1数据点格式错误。。。原因未知
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int pre[];
vector<int>post;
int flag;
void dfs(int l,int r){
if(l>r)
return ;
int ll=l+,rr=r;
if(!flag){
while(ll<=r&&pre[ll]<pre[l])
++ll;
while(rr>l&&pre[rr]>=pre[l])
--rr;
}
else{
while(ll<=r&&pre[ll]>=pre[l])
++ll;
while(rr>l&&pre[rr]<pre[l])
--rr;
}
if(ll!=rr+)
return ;
dfs(l+,rr);
dfs(ll,r);
post.push_back(pre[l]);
}
int main(){
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>pre[i];
flag=;
dfs(,n);
if(post.size()<n){
flag=;
post.clear();
dfs(,n);
}
if(post.size()<n)
cout<<"NO";
else{
cout<<"YES\n";
cout<<post[];
for(int i=;i<post.size();++i)
cout<<" "<<post[i];
}
return ;
}
【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)的更多相关文章
- PAT 甲级 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 bina ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 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 proper ...
- 1043 Is It a Binary Search Tree (25分)(树的插入)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- 【PAT】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 ...
- PAT甲级——A1043 Is It a Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
随机推荐
- idea中使用Data Source and Drivers时,如果使用自己自定义的jar包
- C++算法导论第九章O(n)期望选择序列第i小的数字
#include<iostream> #include<vector> #include<algorithm> #include<time.h> usi ...
- redis安装与基本使用
什么是Redis 什么是NoSQL 介绍Redis之前,先了解下NoSQL (Not noly SQL)不仅仅是SQL 属于非关系型数据库:Redis就属于非关系型数据库 传统的Mysql ,orac ...
- 关于MultiAutoCompleteTextView的用法:多文本匹配
- 【 SSH 整合】Spring、Struts、Hibernate基本整合
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- php相关问题学习(以备面试)
1.原味地址:[ http://www.yiichina.com/tutorial/57 ] 注:本文转自 http://www.icultivator.com/p/5535.html 整理了一份PH ...
- python 在linux上面安装beautifulsoup4(bs4) No module named 'bs4'
续费了我的服务器 重做系统成了Linux服务器 然后想把Windown上的Python脚本放上去运行 但是出现了 No module named 'bs4' 的问题 pip install bs4 试 ...
- redis(一)动态字符串
redis 动态字符串 概述 Sda(Simple Dynamic String) 简单动态字符串是 redis中用来表示字符串的结构,而不是传统 C 字符串. 主要的特点就是Sda要做到高效和 二进 ...
- dp(小猪存钱罐)
B - Piggy-Bank 在acm能够做任何事情之前, 必须编制预算并获得必要的财政支持.这一行动的主要收入来自IBM.这个想法其实很简单,每当一些会员有一点小 ...
- redis-py相关
一 redis客户端命令 cmd进入redis客户端管理程序路径xx:\windows redis\redis-2.4.0-win32-win64\64bit 执行:redis-cli.exe -h ...