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. 这道 ...
随机推荐
- Java核心技术 卷Ⅰ 基础知识(2)
第四章 对象与类 基于类的访问权限 静态域 类的设计技巧
- 什么是MongoDB、特点、历史、下载和工具
什么是MongoDB ?MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展 ...
- 访问mysql出现“Access denied for user root@localhost”(using password:NO)解决方案
首先声明,出现这个提示的原因有很多,以下只针对我遇到的一种情况 使用解压缩版安装mysql的时候,data文件夹是自己新建的,my-default.ini也是自己配置的,这时直接启动mysql服务的时 ...
- codevs1028花店橱窗布置(费用流)
这几天刚学了费用流,找到了这道题来练一练手. 题目: 题目描述 Description 假设以最美观的方式布置花店的橱窗,有F束花,V个花瓶,我们用美学值(一个整数)表示每束花放入每个花瓶所产生的美学 ...
- U3D简单得换装技术
四个类完成,前提是 资源得名字配合 UI按钮点击响应类 using UnityEngine; using System.Collections; public class ButtonClickHan ...
- mysql数据一致性检查及修复
percona-toolkit-2.2.20-1.noarchmysql 5.6.29-logmaster:192.168.166.129slave:192.168.166.131 一.创建数据库校验 ...
- getResourceAsStream和getResource的用法
用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大家最经常用的,就是用JAVA的File类,如要取得 D:/test.txt文件,就会这样用File file ...
- c++ 常见问题之string
string初始化: 拷贝初始化: = 初始化变量,编译器把等号右侧的初始值拷贝到新创建的对象 string s = "stduent"; string s = s1; 直 ...
- ajax加php实现简单的投票效果
废话少说,作为一个前端猿,首先上前端的代码. 1.上html代码: <!DOCTYPE html> <html> <head lang="en"> ...
- NYOJ 299
(前言:这是一道关于矩阵快速幂的问题,介绍矩阵快速幂之前,首先看"快速幂"问题. 在前面的博客里有记录到快速幂取模算法,不过总体的思想总是和取模运算混淆在一起,而忽略了" ...