二叉搜索树(hdu3791)
二叉搜索树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2280 Accepted Submission(s): 994
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; typedef struct tree
{
tree *right,*left;
int num;
}tree;
tree *root; int a[],b[],count=; tree *creat(int x)//建树
{
tree *t=(tree *)malloc(sizeof(tree));
t->right=NULL;
t->left=NULL;
t->num=x;
return t;
} tree *inster(tree *s,int x)//插入
{
tree *t;
if(s==NULL)
{
t=creat(x);
s=t;
}
else
{
if(x<=s->num)
s->left=inster(s->left,x);
else
s->right=inster(s->right,x);
}
return s;
}
void libian(tree *root)
{
if(root!=NULL)
{
b[count++]=root->num;
libian(root->left);
libian(root->right);
}
}
int main()
{
int n;
while(scanf("%d",&n)>&&n)
{
count=;
root=NULL;
char str[];
scanf("%s",str);
int len=strlen(str);
int i,j;
for(i=;i<len;i++)
{
int tmp=str[i]-;
root=inster(root,tmp);
}
libian(root);
for(i=;i<len;i++)
a[i]=b[i];
while(n--)
{
count=i=;
scanf("%s",str);
root=NULL;
for(i=;i<len;i++)
{
int tmp=str[i]-;
root=inster(root,tmp);
}
libian(root);
for(i=;i<len;i++)
if(a[i]!=b[i])
{
printf("NO\n");
break;
}
if(i>=len)
printf("YES\n");
}
}
return ;
}
前几天放假,玩了几天,接下来继续学。。。。努力!
二叉搜索树(hdu3791)的更多相关文章
- HDU3791二叉搜索树(二叉树)
Problem Description 判断两序列是否为同一二叉搜索树序列 Input 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束.接下去一行是一 ...
- hdu3791二叉搜索树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3791 题意:给定一个n(多组,n为0时结束),给一个串和n个串,分别判断n个串按序列构建的二叉搜索树和 ...
- [数据结构]——二叉树(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. 这道 ...
随机推荐
- 基于emoji 国际通用表情在web上的输入与显示的记录
定义: emoji 即国际通用表情 场景: 1,ios,android,wp上emoji表情输入与显示 2,web也需作为支撑平台对emoji表情就行输入与显示(解析) 问题: 1,app端输入的表情 ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 一起做OJ-环境搭建
这几天,看到OJ火了起来,尤其是OnlineJudge(QingDaoU)和HUSTOJ. 作为正在花大力研究PHP和Bootstrap的我,看到了,当然不会甘心. 于是,我决定把学到的知识用在编写O ...
- 【BZOJ3551】 [ONTAK2010]Peaks加强版
BZOJ3551 [ONTAK2010]Peaks加强版 Solution Kruscal重构树后发现可以对于小于的离散化然后倍增+主席树找到上一个的可行解. 然后就可以了. 如果数组开的不好,容易在 ...
- Spring Cloud实践之服务注册与发现Eureka
一.简述: 服务提供者producer与服务消费者consumer都注册到eureka server,然后服务consumer在其应用内直接调用producer的服务名来调用服务,而不是像之前一样调用 ...
- com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
出现上述bug的原因如下: 在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为. 禁止方式如下:在application.propertie ...
- poi 读取使用 Strict Open XML 保存的 excel 文档
poi 读取使用 Strict Open XML 保存的 excel 文档 某项目有一个功能需要读取 excel 报表内容,使用poi读取时报错: 具体错误为: org.apache.poi.POIX ...
- 7. Bagging & Random Forest
通过前面集成学习的介绍我们知道,欲得到泛化性能强的集成学习器,集成中个体学习器应尽量相互独立:虽然“独立”在现实任务中无法做到,但可以设法使基学习器尽可能具有较大差异. 1. Bagging 自助采样 ...
- 人工智能-机器学习之Selenium(chrome驱动,火狐驱动)
selenium是一个用于web应用程序测试的工具,Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla Suite等 ...
- redis缓存存在的隐患及其解决方案
redis缓存1.缓存穿透 1>.什么是缓存穿透? 业务系统需要查训的数据根本不存在,当业务系统查询时, 首先会在缓存中查训,由于缓存中不存在,然后在往数据 库中查,由于该数据在数据库中也不存在 ...