PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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)-二叉搜索树的更多相关文章
- 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 (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- 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 ...
- PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- 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)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)
①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...
随机推荐
- JFreeChart框架中生成饼状图上怎样显示数据 [问题点数:40分,结帖人GreenLawn]
我用JFreeChart框架生成饼状图,但想把数据信息在饼图上显示,是在饼图内部(即圆内)显示!怎样实现啊?? 去掉lablepieplot.setLabelGenerator(null);去掉线p ...
- oc 的 协变性与逆变性
?协变性与逆变性是类型关系在范畴论的定义.是类型的继承关系在高阶类型中的定义? __kindof只是在统一继承体系下方便了类型转化,提供了使用时语法上的便捷:但是对于类型转换是否正确不做判定: kin ...
- 为什么Github要把代码合并请求称为pull request而不是push request?
问题: 我的理解是:我做了一些修改,我请求把我的修改push到你的仓库,然后你review一下我的代码,如果没问题就接受请求merge,这样的话叫做push request岂不是更合适?因为这个操作是 ...
- RC Calculation
scenario 定义中包括 Mode.Corner.RC 其中 Corner (PVT)用于计算 cell delay 而 RC 用于计算 net delay 本文简要介绍如何使用 RC 参数来计算 ...
- JAVA springmvc参数
一.简单参数: package jd.com.contronller; import jd.com.projo.goods; import org.springframework.stereotype ...
- Vue表单绑定(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- Liunx-mkdir命令
1. 新建一个文件夹 one 2. 新建三个文件夹three,four,five 3. 新建一个多层级文件夹 201904/a/01
- 20155216 Exp6 信息搜集与漏洞扫描
Exp6 信息搜集与漏洞扫描 实践内容 信息搜集 whois查询 使用whois查询域名注册信息,查询百度服务器(进行whois查询时去掉www等前缀,因为注册域名时通常会注册一个上层域名,子域名由自 ...
- 20155227《网络对抗》Exp8 Web基础
20155227<网络对抗>Exp8 Web基础 实验内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建库. ...
- 20155237方自晨 实验四android开发基础
提交点一 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: 安装 A ...