博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6789220.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~

题意:给出一个序列,问你是否是一棵二叉搜索树或者是其镜像的前序遍历。
并且输出其后序遍历。

一开始没考虑到只有一个子树...比如
BST
7
8 11 9 8 10 13 12
MIRROR
7
8 11 13 12 9 10 8
所以要先判断是否为BST,不是的话再判断是否为mirror BST
都不是输出NO

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
#define LEFT 1
#define RIGHT 2 using namespace std;
const int maxn=;
int n;
int seq[maxn];
int cnt=;
struct Node{
int left=-;
int right=-;
int val;
}node[maxn];
/**
判断对应区间[l,r]是否为二叉搜索树
fa为其父亲节点
leftOrRight表示该子树为父亲节点的左孩子还是右孩子
*/
bool solveBST(int l,int r,int fa,int leftOrRight){
//printf("l:%d r:%d fa:%d leftorRight:%d\n",l,r,fa,leftOrRight);
if(l>r)
return true;
if(l==r){
node[cnt].val=seq[l];
if(leftOrRight==LEFT){
node[fa].left=cnt;
}
else{
node[fa].right=cnt;
}
cnt++;
return true;
}
node[cnt].val=seq[l];
int val=seq[l];
int id=cnt; if(fa!=-){
if(leftOrRight==LEFT){
node[fa].left=cnt;
}
else{
node[fa].right=cnt;
}
}
cnt++;
int i;
for(i=l+;i<=r;i++){
if(seq[i]>=val){
break;
}
}
for(int j=i+;j<=r;j++){
//如果右子树应该都>=val,如果有小于的,那么就不是BST
//printf("seq[j]:%d val:%d\n",seq[j],val);
if(seq[j]<val){
return false;
}
}
if(!solveBST(l+,i-,id,LEFT))
return false;
if(!solveBST(i,r,id,RIGHT))
return false;
return true;
}
/**
判断对应区间[l,r]是否为二叉搜索树的镜像
fa为其父亲节点
leftOrRight表示该子树为父亲节点的左孩子还是右孩子
*/
bool solveMirrorBST(int l,int r,int fa,int leftOrRight){
//printf("l:%d r:%d fa:%d leftorRight:%d\n",l,r,fa,leftOrRight);
if(l>r)
return true;
if(l==r){
node[cnt].val=seq[l];
if(leftOrRight==LEFT){
node[fa].left=cnt;
}
else{
node[fa].right=cnt;
}
cnt++;
return true;
}
node[cnt].val=seq[l];
int val=seq[l];
int id=cnt; if(fa!=-){
if(leftOrRight==LEFT){
node[fa].left=cnt;
}
else{
node[fa].right=cnt;
}
}
cnt++;
int i;
for(i=l+;i<=r;i++){
if(seq[i]<val){
break;
}
}
for(int j=i+;j<=r;j++){
//mirror的右子树应该都是<val,如果不是,则不是mirror BST
if(seq[j]>=val){
return false;
}
}
if(!solveMirrorBST(l+,i-,id,LEFT))
return false;
if(!solveMirrorBST(i,r,id,RIGHT))
return false;
return true;
}
bool first=true;
void postOrder(int u){
if(u==-)
return;
postOrder(node[u].left);
postOrder(node[u].right);
if(first){
printf("%d",node[u].val);
first=false;
}
else
printf(" %d",node[u].val);
}
void init(){
cnt=;
for(int i=;i<maxn;i++){
node[i].left=node[i].right=-;
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&seq[i]);
}
if(solveBST(,n-,-,-)){
printf("YES\n");
postOrder();
}
else{
init();
if(solveMirrorBST(,n-,-,-)){
printf("YES\n");
postOrder();
}
else{
printf("NO\n");
} }
return ;
}

PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树的更多相关文章

  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 (Advanced Level) 1043. Is It a Binary Search Tree (25)

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

  3. PAT甲题题解-1033. To Fill or Not to Fill (25)-模拟

    模拟先说一下例子,最后为方便起见,在目的地安增加一个费用为0的加油站0 1 2 3 4 5 6 7 87.1 7.0 7.2 6.85 7.5 7.0 7.3 6.0 00 150 200 300 4 ...

  4. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  5. 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)

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

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

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

  8. 1043. Is It a Binary Search Tree (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...

  9. [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)

    ①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...

随机推荐

  1. November 05th, 2017 Week 45th Sunday

    Do not pray for an easy life, pray for the strength to endure a difficult one. 不要祈求安逸的人生,祈求拥有撑过艰难的力量 ...

  2. 【adb命令】在cmd窗口中使用adb install命令安装 中文名字apk报错的解决办法

    1.在cmd窗口中使用adb install命令安装中文名字apk报错,安装英文名字apk就正常,详细报错如下图: 2.查看adb版本号:adb version 3.怀疑是adb版本的原因,尝试换个最 ...

  3. php 两个二维数组重组新数组,数组下标不同

    Array ( [0] => Array ( [PosNum] => 27025008 [start_time] => 20180328164929 [type] => 0 ) ...

  4. 2018-2019-2 20165302 Exp5 MSF基础应用

    1.实验目的 掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路 2.实验内容 一个主动攻击实践; (1分) MS17-010 一个针对浏览器的攻击:(1分) ms14_064 一个 ...

  5. shiro实战系列(十一)之Caching

    Shiro 开发团队明白在许多应用程序中性能是至关重要的.Caching 是从第一天开始第一个建立在 Shiro 中的一流功 能,以确保安全操作保持尽可能的快.   然而,Caching 作为一个概念 ...

  6. PAT B1034 有理数四则运算 (20 分)

    本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前 ...

  7. 第7章 使用寄存器点亮LED灯

    第7章     使用寄存器点亮LED灯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  8. R语言入门 :基本数据结构

    1.向量 向量是R语言中最基本的数据类型,在R语言中没有单独的变量. (1)  创建向量 R语言中可以用 = 或者 <- 来赋值. 向量名 <- 向量 或  向量名 = 向量 向量的创建方 ...

  9. VB 批量重命名文件

    VERSION 5.00 Begin VB.Form Form1 BorderStyle = 3 'Fixed Dialog Caption = "Rename use VB QQ 1009 ...

  10. 20155305乔磊《网络对抗》逆向及Bof基础

    20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...