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. 具有增删改查功能的表格Demo--【BootStrap】

    http://blog.csdn.net/wangmei4968/article/details/48437175

  2. Memcached Cache

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using Memcached ...

  3. 源码方式安装rabbitmq

    由于工作环境中属于内网,yum安装方式不可用,所以需要采用源码进行rabbitmq的安装.rabbitmq是基于erlang环境进行运行的,所以需要先按照erlang环境,才能运行rabbitmq-s ...

  4. iOS工程师 - 简历

    基本信息 姓 名:张学友                  性 别:男 年 龄:28                      学 历:本科 毕业学校:广西师范大学       专 业:通信工程 手 ...

  5. 「ZOJ 1354」Extended Lights Out「高斯消元」

    题意:给定一个\(5\times 6\)的棋盘的\(01\)状态,每次操作可以使它自己和周围四个格子状态取反,求如何操作,输出一个\(01\)矩阵 题解:这题可以通过枚举第一行的状态然后剩下递推来做, ...

  6. [51nod1220] 约数之和(杜教筛+莫比乌斯反演)

    题面 传送门 题解 嗯--还是懒得写了--这里 //minamoto #include<bits/stdc++.h> #define R register #define IT map&l ...

  7. Unity---UGUI入门基础---更新中

    目录 1.UGUI介绍 2.UGUI基础 2.1 Canvas---画布 2.2 Text控件 2.3 Image控件 2.4 RawImage控件 2.5 Button控件 2.6 Toggle控件 ...

  8. EOS 配置mongodb

    本文实现方案:在虚拟机ubuntu上运行单节点的EOS,把数据存储到mongodb中,然后通过本地的windows查看mongodb的数据. 配置如下: 虚拟机: ubuntu 16.04   EOS ...

  9. [LOJ2027] [SHOI2016] 黑暗前的幻想乡

    题目链接 LOJ:https://loj.ac/problem/2027 洛谷:https://www.luogu.org/problemnew/show/P4336 Solution 这题很像[ZJ ...

  10. ubuntu下中文乱码解决

    这个方法只对该用户有效. 方法二:修改/etc/environment,增加以下内容: LANGUAGE=”zh_CN:zh:en_US:en” LANG=zh_CN.GBK