swust oj 1011
二叉排序树的实现和查找
输入
关键字个数n;
关键字集合;
要查找的关键字;
输出
查找成功输出比较的次数,否则输出-1。
样例输入
12
25 18 46 2 53 39 32 4 74 67 60 11
74
样例输出
4
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
typedef int Datetype;
using namespace std;
int x; typedef struct link{
Datetype date;
struct link *lchild;
struct link *rchild;
}tree; void creat(tree *&L, int arr[] ,int l,int r)
{
if(l>r)
{
L=NULL;
return ;
}
int s=(l+r)>>;
L=new tree;
L->date=arr[s];
creat(L->lchild,arr,l,s-);
creat(L->rchild,arr,s+,r);
} void display(tree *&L)
{
if(L!=NULL)
{
cout<<L->date<<" ";
display(L->lchild);
display(L->rchild);
}
} void delet(tree *&L)
{
if(L!=NULL)
{
delet(L->lchild);
delet(L->rchild);
delete(L);
}
} void find(tree *&L, int a)
{
if(L!=NULL)
{
x++;
if(a>L->date)
find(L->rchild,a);
else if(a<L->date)
find(L->lchild,a);
else
return ;
}
if(L==NULL)
x=;
} int main()
{
int arr[];
int n,a;
tree *L;
cin>>n;
for(int i=;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
cin>>a;
creat(L,arr,,n-);
// display(L);
find(L,a);
if(x)
cout<<x;
else
cout<<"-1";
delet(L);
return ;
}
swust oj 1011的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [九度OJ]1011.最大连续子序列
原题链接:http://ac.jobdu.com/problem.php?pid=1011 题目描述: 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ N ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
随机推荐
- 024_mac配置屏保命令
注意吃饭等离开工位的时候养成随时开启屏保的功能,养成信息保护的好习惯,mac如何配置屏幕保护呢? 一. 通过mac"设置"里的"Desktop & Screen ...
- VS 编码规范---- 代码注释设置
一个良好的代码风格在开发过程和后期维护过程中是必不可少的.每次在添加新类的时候都需要添加一些关于类的注释,包括创建时间.创建人.类的功能介绍.修改时间等一系列的信息,以方便以后的查找和快速了解.在Co ...
- Leetcode 992 Subarrays with K Different Integers
题目链接:https://leetcode.com/problems/subarrays-with-k-different-integers/ 题意:已知一个全为正数的数组A,1<=A.leng ...
- java获取application.properties和application.yml配置文件信息
public static void main(String[] args) { String result=getProjectConfig("max-file-size"); ...
- podman(libpod)---github简单记录
这个应该集成了Skopeo 和Buildah. 用于代替docker的工具包,且和cri-o共享后端代码,迟早集成进K8S~~~. (docker肿么办????) github地址: https:// ...
- ansible中的map
ansible中的filter: map ,其实是jinja2中的filter python中 map(func, iter) 返回func与每个元素计算后的迭代器,iter是个可迭代对象 an ...
- 解决问题SyntaxError: Unexpected token import
ES6语法的模块导入导出(import/export)功能,我们在使用它的时候,可能会报错: SyntaxError: Unexpected token import 语法错误:此处不应该出现impo ...
- mysql免解压版的配置
很多朋友在安装mysq解压版l时出现: “mysql 服务无法启动 服务没报告任何错误” 以前我安装时也是遇到这样的问题: 其实mysql在5.6后就没有了data目录,很多朋友按照以前的版本安装会去 ...
- Excel—图表函数
OFFSET(引用函数)它可以找一个区域,把一个区域的值都拿回来 函数语法:OFFSET(从哪个单元格开始,这个单元格下移多少行开始,然后现在的位置再往右多少列开始,要取的值是现在的位置开始下移多少行 ...
- Python2出现SyntaxError: Non-ASCII character '\xe5' in file *******
在使用Python2编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息:SyntaxError: Non-ASCII character '\xe5' in file ******* ...