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. 这是二叉搜索树吗?的更多相关文章

  1. 天梯 L2 这是二叉搜索树吗?

    L2-004 这是二叉搜索树吗? (25 分) 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的 ...

  2. 有序链表转换二叉搜索树(LeetCode)

    将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...

  3. hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)

    二叉搜索树 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  4. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  5. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. [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 ...

  10. [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. 这道 ...

随机推荐

  1. GourdScan & sqlmapapi

    0x01  Windows下配置GourdScan 0x0101  GourdScan项目地址:https://github.com/code-scan/GourdScan  PHP环境   +   ...

  2. wpf使用devexpress RibbonControl实现导航窗体

    实现如下效果 <Window xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"        ...

  3. TCP/IP协议中backlog参数

    TCP建立连接是要进行三次握手,但是否完成三次握手后,服务器就处理(accept)呢? backlog其实是一个连接队列,在Linux内核2.2之前,backlog大小包括半连接状态和全连接状态两种队 ...

  4. ios_swift开发资源整理

    目录 1.苹果官方资源 2.国内外视频网站推荐 3.中文文档 4.demo网站 5.开发工具推荐 6.国内外开发网站论坛 7.技术博客推荐 8.书籍推荐 9.第三方框架推荐 10.第三方发布平台 11 ...

  5. 微软sqlHelper

    //微软的SQLHelper类(含完整中文注释) using System; using System.Data; using System.Xml; using System.Data.SqlCli ...

  6. 主成分分析(Principal components analysis)-最大方差解释

    原文:http://www.cnblogs.com/jerrylead/archive/2011/04/18/2020209.html 在这一篇之前的内容是<Factor Analysis> ...

  7. 使用 voluptuous 校验数据

    在 Python 中,我们经常需要对参数进行校验,这是我们有好多种方法,例如写很多 if 啊,或者写正则表达式啊等等,技巧高的人可以写得很巧妙,而技巧一般的人呢,可能会写得很冗长,例如我,经常就不能很 ...

  8. [UWP小白日记-6]页面跳转过度动画

    前言 在学习中发现页面导航默认是没有过度动画的,直接就导航过去太粗暴了( ̄へ ̄),于是打算上动画结果不言而喻自己进了坑完全不懂动画,然后就是各种疯狂(´・_・`)的搜索资料看了后终于有点头绪. 再后来 ...

  9. BAPI_GOODSMVT_CREATE 移动类型311 CODE = '04' 代码

    DATA: MAT_DOC LIKE BAPI2017_GM_HEAD_RET-MAT_DOC.      "物料凭证编号   DATA: GMHEAD LIKE BAPI2017_GM_H ...

  10. VS中,如何将存在于解决方案里,但是没有显示出来的文件(或文件夹)显示到项目中。

    不知道有没有人跟我一样,刚开始接触VS的时候,没有通过"右键->添加"产生文件,而是直接一些文件或者文件夹建在了项目的本地目录中. 导致最后这些文件(或文件夹)无法在项目中显 ...