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. 这道 ...
随机推荐
- Andrew Ng机器学习入门——线性回归
本人从2017年起,开始涉猎机器学习.作为入门,首先学习的是斯坦福大学Andrew Ng(吴恩达)教授的Coursera课程 2 单变量线性回归 线性回归属于监督学习(Supervise Learni ...
- saltstack布署实践 【安装】
借用链接http://www.cnblogs.com/liuyansheng/p/6094122.html的安装方式,我再同步一下其它操作系统的安装方式,由原Docker官网拷贝 Ubuntu1404 ...
- openstack私有云布署实践【9.1 Glance镜像管理(科兴环境)】
首先登录kxcontroller1创建kx_glance数据库,并赋于远程和本地访问的权限. mysql -u root -p CREATE DATABASE kx_glance; GR ...
- 如何使用jQuery动态的在body里添加script标签?
var script = document.createElement('script'); script.type = 'text/jacascript'; script.src = 'url'; ...
- 查增删改MySQL数据库固定模式
省略相关包的导入... public class Base { public static Connection connection = null; public static PreparedSt ...
- java操作mongodb——更新数据
Java中可以通过updateOne,updateMany,replaceOne方法进行集合的文档更新.但是 _id 是不能更新的 updateOne只会更新一条数据,即使通过Filters.lt(& ...
- webkit框架的使用
// // JSViewController.m // Library // // Created by 朱逸 on 16/7/7. // Copyright © 2016年 朱逸. All righ ...
- jsp/servlet中的编码问题
首先声明以下只是我个人的看法,有部分观点与网上人云亦云的观点不一样,不过凡事必恭亲,我还是相信自己测试的结果 推荐一个很好地URL编码详解http://www.ruanyifeng.com/blog/ ...
- PowerTool(杀毒辅助工具) V4.6 中文免费绿色版
软件名称: PowerTool(杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 968KB图片预览: 软件简 ...
- vim的配置文件参数
环境:kali linux vim的配置文件存放在/etc/vim目录中,配置文件名为vimrc set nocompatible "去掉有关vi一致性模式,避免以前版本的bug和局限&qu ...