04-树7. Search in a Binary Search Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For example, following {1, 4, 2, 3} we can find 3 from a binary search tree with 1 as its root. But {2, 4, 1, 3} is not such a path since 1 is in the right subtree of the root 2, which breaks the rule for a binary search tree. Now given a sequence of keys, you are supposed to tell whether or not it indeed correspnds to a searching path in a binary search tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=100) which are the total number of sequences, and the size of each sequence, respectively. Then N lines follow, each gives a sequence of keys. It is assumed that the keys are numbered from 1 to M.

Output Specification:

For each sequence, print in a line "YES" if the sequence does correspnd to a searching path in a binary search tree, or "NO" if not.

Sample Input:

3 4
1 4 2 3
2 4 1 3
3 2 4 1

Sample Output:

YES
NO
NO

提交代码

法一:

如果是二叉查找树,树上除了叶结点,每个节点的左右子树必有一棵空,另一棵不空。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
struct node{
int v;
node *l,*r;
node(){
l=r=NULL;
}
};
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m;
scanf("%d %d",&n,&m);
while(n--){
queue<int> q;
int i,num;
node *p,*pp,*h=NULL;
for(i=;i<m;i++){
scanf("%d",&num);
q.push(num);
}
h=new node();
h->v=q.front();
q.pop();
for(i=;i<m;i++){
num=q.front();
q.pop();
p=h;
while(p){
pp=p;//pp point to the father of p
if(num>p->v&&p->l==NULL){
p=p->r;
}
else{
if(num<p->v&&p->r==NULL){
p=p->l;
}
else
break;//防止元素相等
}
}
if(p==NULL){
p=new node();
p->v=num;
if(num>pp->v){
pp->r=p;
}
else{
pp->l=p;
}
}
else{
break;
}
}
if(i==m){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

法二:

先按题意建树,得到树后,用最后输入的数进行树的遍历,如果最后经过m次找到匹配的叶结点,说明序列正确;否则,序列错误。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
struct node{
int v;
node *l,*r;
node(){
l=r=NULL;
}
};
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m;
scanf("%d %d",&n,&m);
while(n--){
queue<int> q;
int i,num,wantfind;
node *p,*pp,*h;
for(i=;i<m;i++){
scanf("%d",&num);
q.push(num);
}
wantfind=num; //最后一个
h=new node();
h->v=q.front();
q.pop();
for(i=;i<m;i++){ //建树
num=q.front();
q.pop();
p=h;
while(p){
pp=p;//pp point to the father of p
if(num>p->v){
p=p->r;
}
else{
p=p->l;
}
}
p=new node();
p->v=num;
if(num>pp->v){
pp->r=p;
}
else{
pp->l=p;
}
}
int count=;
p=h;
while(p){
if(wantfind>p->v){
p=p->r;
}
else{
p=p->l;
}
count++;
}
if(count==m){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

pat04-树7. Search in a Binary Search Tree (25)的更多相关文章

  1. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  2. [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

    Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...

  3. 【Leetcode_easy】700. Search in a Binary Search Tree

    problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...

  4. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  5. 【LeetCode】700. Search in a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  6. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  7. Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  8. Lintcode: Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  9. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

随机推荐

  1. 将Winform程序及dll打包成可执行的exe

    使用场景 通常开发的Winform程序,引用了其他类库后,在输出目录下都会产生很多DLL文件,exe执行时必须依赖这些DLL.想要Winform程序只有一个可执行exe文件,又不想打包成安装包,就可以 ...

  2. 一个android dex 转java源码工具

    和dex2jar,smali2java比起来,这个工具至少结果是正确的,前两者对于循环,异常等的处理明显逻辑就是错误的. 该小工具是基于androguard制作,本来是想自己写一个,后来一找居然有现成 ...

  3. c# ftp创建文件(非上传文件)

    c#  ftp创建文件(非上传文件) 一.奇葩的故事: 今天项目中遇到这么个奇葩的问题,ftp文件传输完成后要在ftp目录下另一个文件夹下创建对应的空文件,听说是为了文件的完整性,既然这么说,那么就必 ...

  4. 在Android中使用FlatBuffers(中篇)

    本文来自网易云社区. FlatBuffers.Protobuf及JSON对比测试 FlatBuffers相对于Protobuf的表现又如何呢?这里我们用数据说话,对比一下FlatBuffers格式.J ...

  5. P与NP问题详解

    P,NP,NPC问题,这或许是众多OIer最大的误区之一. 本文就为大家详细讲解如上三个问题. 前序: 你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜了,这已经被证明是NP问题了”之 ...

  6. 【1】循序渐进学 Zabbix :初识与基础依赖环境搭建( LNMP )

    写在前面的话 运维监控是一个很大的话题,在这一块个人接触的比较突出的服务主要有 Nagio 和 Zabbix 两款.而这几年跳过的公司中,Zabbix 一直都是首选且唯一选择,Nagios 没遇到. ...

  7. Django之后台管理一

    所有的网站都有一个管理后台来对所有的网站数据进行管理.那么Django的后台管理是如何进行的.在网页中输入http://127.0.0.1:8001/admin.得到如下的登录界面 在这里可以看到管理 ...

  8. 与HDFS交互- By web界面

    开启方式 linux系统 eg :ubuntu 浏览器 eg:firefox HDFS的web管理界面地址:localhost:50070/ 具体理解有待后续学习....

  9. luogu2480 [SDOI2010]古代猪文

    link 题意一开始没TM读懂... 就是给定一个\(G\le10^{10},N\le10^9\),求\(G^{\sum_{d|n}{n\choose d}}\),对999911659取模 由于999 ...

  10. An error has occurred. See error log for more details. java.lang.NullPointerException

    今天重新新建一个eclipse的空间,就报了这么一个错误,百度找了很多,发现不对头,都不行.后来点开错误一看,原来是web.xml里面没有文件头造成的. 解决办法:只需要在web.xml文件里添加文件 ...