L2-004. 这是二叉搜索树吗?
L2-004. 这是二叉搜索树吗?
题目链接:https://www.patest.cn/contests/gplt/L2-004
这题我的方法是先递归判定是不是二叉搜索树(镜像),再建树输出。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
bool isST(int a[],int l,int r){
if(l>=r)return ;
int key=r;
for(int i=l+;i<=r;++i){
if(a[l]<=a[i]){
key=i;
break;
}
}
for(int i=key+;i<=r;++i)
if(a[l]>a[i])return ;
return isST(a,l+,key-)&&isST(a,key,r);
}
bool isMirST(int a[],int l,int r){
if(l>=r)return ;
int key=r;
for(int i=l+;i<=r;++i){
if(a[l]>a[i]){
key=i;
break;
}
}
for(int i=key+;i<=r;++i)
if(a[l]<=a[i])return ;
return isMirST(a,l+,key-)&&isMirST(a,key,r);
}
struct node{
int vul;
node *l,*r;
};
node *T=NULL;
node* st(node *t,int key){
if(t==NULL){
node *temp=(node*)malloc(sizeof(node));
temp->vul=key;
temp->l=NULL;
temp->r=NULL;
return temp;
}
if(key>=t->vul)t->r=st(t->r,key);
else t->l=st(t->l,key);
return t;
}
node* Mirst(node *t,int key){
if(t==NULL){
node *temp=(node*)malloc(sizeof(node));
temp->vul=key;
temp->l=NULL;
temp->r=NULL;
return temp;
}
if(key<t->vul)t->r=Mirst(t->r,key);
else t->l=Mirst(t->l,key);
return t;
}
void printT(node *t){
if(t==NULL)return;
printT(t->l);
printT(t->r);
if(t!=T)printf("%d ",t->vul);
}
int main(void){
freopen("in.txt","r",stdin);
int a[];
memset(a,,sizeof(a));
int n;
scanf("%d",&n);
if(n==){
printf("YES\n");
return ;
}
for(int i=;i<n;++i)
scanf("%d",&a[i]);
bool flag1=isST(a,,n-);
bool flag2=isMirST(a,,n-);
if(!(flag1||flag2)){
printf("NO\n");
return ;
}
else if(flag1)for(int i=;i<n;++i)T=st(T,a[i]);
else if(flag2)for(int i=;i<n;++i)T=Mirst(T,a[i]);
printf("YES\n");
printT(T);
printf("%d",T->vul);
printf("\n");
return ;
}
如有更好的方法,希望不吝赐教!!
L2-004. 这是二叉搜索树吗?的更多相关文章
- 天梯 L2 这是二叉搜索树吗?
L2-004 这是二叉搜索树吗? (25 分) 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的 ...
- 有序链表转换二叉搜索树(LeetCode)
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
二叉搜索树 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- dubbo学习笔记
一.zookeeper在Dubbo中扮演角色 流程:1.服务提供者启动时向/dubbo/com.foo.BarService/providers目录下写入URL2.服务消费者启动时订阅/dubbo/c ...
- IDEA导入maven项目的步骤
其它的就不多说,直接开始咱们的步骤. 首先,你的装有IDEA,我的是IDEA15的,其次就是你的maven插件来,不过,这些基本的插件,IDEA里基本都给咱下载好了,你就可以直接配置maven.可以去 ...
- ACM第二题 生理周期
人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易 ...
- Django 同步数据库命令syncdb,makemigrations,migrate
syncdb只支持新增模型到数据库中 2条命令 你将使用2条命令进行迁移和操纵数据库schema: migrate,用来使迁移生效,以及未生效时报告它们的状态. makemigrations,用来根据 ...
- 自己找到的一些比较实用比较好看的前端特效。很多是前面提供的jquery网站的
http://www.cnblogs.com/zhangzongle/p/6034394.html webservicehttp://blog.csdn.net/wowkk/article/detai ...
- Ubuntu 16.04 搭建Android开发环境
1.Installing Java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get inst ...
- 【NOIP2013】DAY1题解+代码
T1 傻逼快速幂,敲敲就过了. 我跟你们讲个笑话当时我以为这个数据范围过不了于是想出了求GCD再推规律什么的magic方法中途还咨询了某个学长. 然后怎么想都是不可做. ……直到我发现我昨年的代码一个 ...
- Linux下的数据监控工具
Vmstat Vmstat,virtual memmory statistics(虚拟内存统计),主要是对操作系统的内存信息.进程状态.cpu活动等进行监视,但是它不能对某个进程进行深入的分析. Pr ...
- hdu1035
#include<stdio.h>#include<string.h>int step,n,m;int a[1010][1010];char map[11][11];void ...
- 浙大pat1013题解
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...