题意:

输入一个正整数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. ListVIew中包含水平滑动控件,左右滑动时容易触发上下滑动

    自定义ListView import android.content.Context;import android.util.AttributeSet;import android.view.Moti ...

  2. 关于Excel,你一定用的到的36个Python函数

    从Excel到Python:最常用的36个Pandas函数关于Excel,你一定用的到的36个Python函数 本文涉及pandas最常用的36个函数,通过这些函数介绍如何完成数据生成和导入.数据清洗 ...

  3. Java-POJ1001-求高精度幂

    参考博客:https://www.cnblogs.com/downrainsun/p/11041960.html package poj.ProblemSet; import java.math.Bi ...

  4. 16进制 32进制 base64之间的区别

    Base64: 包含大写字母(A-Z),小写字母(a-z),数字(0-9)以及+/; Base32: 而Base32中只有大写字母(A-Z)和数字234567: Base16: 而Base16就是16 ...

  5. hibernate和mybatis出现配置文件xml的文件报错Multiple annotations found at this line(转)

    hibernate中的xml配置文件Multiple annotations found at this line,出现这个红叉报错,直接是把 <?xml version="1.0&q ...

  6. eureka-获取服务列表(各种状态)

    在刚开始做的时候也搜了下搜到的大多是下面的第一种方法,这种方法很简单,但并不是Eureka展示的那个服务列表,他只包括了注册证成功的,或者说eureka中状态为“Up”的实例列表,对于down掉的实例 ...

  7. 精简版logging

    # coding=utf-8 import logging import time import os import logging.handlers import re def logger(sch ...

  8. 详解Django中Request对象的相关用法

    原文链接:http://www.jb51.net/article/69624.htm 1.从Request对象中获取数据 我们在第三章讲述View的函数时已经介绍过HttpRequest对象了,但当时 ...

  9. Redis-复制(MasterSlave)

    Redis的复制(Master/Slave) 是什么: 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave ...

  10. 阿里云服务器-2.使用putty连接

    1.下载PuTTY 进入官网 点击我跳转 点击here 看自己电脑是多少位选择下载 2.安装 一直点击下一步就行 这里可以选择也可以不选择,这会在创建桌面快捷文件 看图注意事项 这里也可以用密匙,先去 ...