题意:

输入一个正整数N(<=1000),接下来输入N个点的序号。如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO。

trick:

for(auto it:post)
cout<<it<<((it!=post[n-1])?" ":"");

这样输出会使第0,1数据点格式错误。。。原因未知

cout<<post[0];
for(int i=1;i<post.size();++i)
cout<<" "<<post[i];
这样同样正确

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的先序遍历并输出后序遍历)的更多相关文章

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

  2. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

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

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

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

  6. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

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

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

  9. PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)

    简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...

随机推荐

  1. Linux 中查看可用的网络接口

    目录 Linux 中查看可用的网络接口 在 Linux 中找到可用的网络接口 title: Linux 中查看可用的网络接口 date: 2020/2/25 16:56:36 toc: true -- ...

  2. selenium chromedriver退出报错

    记录使用python调用chromedriver时遇到的问题 代码: #!/usr/bin/env python #-*- coding:utf-8 -*- # author : fy # versi ...

  3. php商城数据库的设计 之无限分类

    商品分类,使用无限分类 即: -------如何创建数据表 pid---父级分类id,如果是顶级分类则为0 path---1,用户分类的排序 . 排序示例: 实现逻辑:获取type表的所有分类,ord ...

  4. Java进阶学习(4)之继承与多态(下)

    多态变量和向上造型 多态变量 子类和子类型 类定义了类型 子类定义了子类型 子类的对象可以被当作父类的对象来使用 赋值给父类的变量 传递给需要父类对象的函数 放进存放父类对象的容器里 多态变量 Jav ...

  5. [NLP]Transformer模型解析

    简介[2] Attention Is All You Need是2017年google提出来的一篇论文,论文里提出了一个新的模型,叫Transformer,这个结构广泛应用于NLP各大领域,是目前比较 ...

  6. ISE-Backup Data Type

    Cisco ISE allows you to back up data from the Primary PAN and from the Monitoring node. Back up can ...

  7. ACL与OSPF综合实验

    OSPF与ACL 综合实验   拓扑图如下: 分析: 配置基本配置: R1: R2: R3: 2.配置OSPF: R1: R2: R3: IT: 设置IT的ip 并划分到ospf2区域 3.配置ACL ...

  8. 入门chrome插件开发教程和经验总结,一篇就搞掂!

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_44244857/articl ...

  9. Kettle-User Defined Java Class使用-大写转换

    一.大写转换 (1)步骤(表输入-Java脚本-表输出) (2)配置 1)表输入 2)java脚本 public boolean processRow(StepMetaInterface smi, S ...

  10. Yii2掉index.php?r=

    普通 首先确认apache2配置 1. 开启 apache 的 mod_rewrite 模块 去掉LoadModule rewrite_module modules/mod_rewrite.so前的“ ...