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. 例如, 给定二叉搜索树: 在上述示例 ...
随机推荐
- MySQL基础之 索引
MySQL索引讲解 索引的好处: MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么 ...
- arm 开发板更新 gcc/gcc++ | Debain 更新 gcc,无需编译直接更新 gcc
4我的板子是 Orange pi 3,只能以 卧槽来形容... 我是搞.net core的,这板子死活搞不了. 刷的是Debain系统. 说实话,这个板子不错,可就是官方的系统实在不敢恭维,内核旧,软 ...
- November 14th, 2017 Week 46th Tuesday
Eternity is said not to be an extension of time but an absence of time. 永恒不是时间的无限延伸,而是没有时间. What is ...
- 复杂json的解析:jsonobject与jsonArray的使用
String parameter = { success : 0, errorMsg : "错误消息", data : { total : "总记录数", ro ...
- 有关于分布式缓存Hazelcast
如果在内网段中部署或者启动缓存服务.不能存在相同的组名称.如同使用dubbo一样,会导致无法连接到缓存节点
- 向大家推荐一个在.Net下使用C#语言和Managed DirectX 9开发游戏的视频教程
视频教程:3D游戏开发步步高系列课程(微软课堂).美中不足的是视频的声音和画面不太对应.专心的听声音,听老师讲解吧. PPT和源码下载:3D游戏开发步步高系列课程-PPT和源码 网址链接:3D游戏开发 ...
- JS重构分页
JS重构分页 很早以前写过一个Jquery分页组件,但是当时写的组件有个缺点,当时的JS插件是这样设计的:比如:点击 --> 查询按钮 ---> 发ajax请求 返回总页数和所有数据, ...
- 简单的Tab切换组件
由于代码都有注释,所以不多加解释,大家都知道的.直接贴代码: 代码如下: /** * 简单的Tab切换 * 支持可配置项 如下参数 */ function Tab(){ this.config = { ...
- PAT B1033 旧键盘打字 (20 分)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在 2 行中分别给出坏掉的那些键.以及应该输入 ...
- Windows/Linux获取当前运行程序的绝对路径
windows 获取当前运行程序的绝对路径(.exe) GetModuleFileNameA()函数获取绝对路径,不过文件路径中的反斜杠需要进行替换. ]; GetModuleFileNameA(NU ...