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 <stdio.h>
//搜索树要求路径中任一元素的右边全部元素都要同一时候大于它或小于它;
//直接遍历每一个元素,比較右边元素大小关系。时间复杂度为O(n^2)。超时;
//O(n)方法:从路径尾部開始,分别维护两个变量:当前尾部元素的最大值和最小值。 int judgePath(int *path, int n) {
int min = path[n - 1], max = path[n - 1];
for (int i = n - 2; i >= 0; --i) {
if (path[i] > max) //假设当前元素比最大值还大。说明后面的路径是当前元素的左子树。可行
max = path[i]; //更新最大值
else if (path[i] < min)
min = path[i];
else //当前元素介于最大值与最小值之间。不可行
return 0;
}
return 1;
}
int main() {
// freopen("test.txt", "r", stdin);
int n, m;
scanf("%d%d", &n, &m);
while (n--) { //n个測试用例
int path[100] = {};
for (int i = 0; i < m; ++i) {
scanf("%d", &path[i]);
}
if (judgePath(path, m))
printf("YES\n");
else
printf("NO\n");
} return 0;
}

题目链接:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%917

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

  1. pat04-树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. How to let TVirtualStringTree to display an icon in disabled state?

    How to let TVirtualStringTree to display an icon in disabled state? I need to display files in a dir ...

  2. Boost Converter

    Single Inductor Buck-Boost Converter in Tiny WCSP The TPS63036 is a non inverting buck-boost convert ...

  3. 三分钟教你学Git (四)之紧急救助

    假设你不小心git reset --hard HEAD^ 然后这个commit又没有在别的git仓库中,怎么办?是不是这次改动就丢了呢? 当然不是,git为我们每次都历史都保留了reference l ...

  4. IDE的文件和代码模板

    设置IDE的的模板,可以在创建文件的时候,自动产生模板内容,模板里可以 模板头设置: # -*- coding: utf- -*- """ --------------- ...

  5. iOS:搜索栏控件UISearchBar and SearchDisplayController的使用

    UISearchBar and SearchDisplayController控件: 这是一个带搜索栏和搜索显示控制器的控件,前面的SearchBar是一个搜索栏,它提供一个输入搜索条件的类似于文本框 ...

  6. Orchard运用 - 整合多说评论插件

    曾经我在一随笔讲述如何整合第三方Disqus评论插件,不过这一插件不是本土,对中国客户毕竟有点别扭.比如这一随笔就提到为啥要选择多说 - 另外一个国内比较知名的评论插件. 今天跟大家分享如何用最简单的 ...

  7. 如何在android模拟器中导入搜狗输入法?

    1.下载输入法程序,如:sogouinput_android_1.6_sweb.apk 2.然后cmd进入sdk的tools(有的是platform-tools)目录,输入adb install C: ...

  8. Linux laravel安装

    第一步:安装php套件 目前为止laravel是5.1版本,需要对php有要求,要php5.59以上 The Laravel framework has a few system requiremen ...

  9. Vim的使用 区域选择

    1.Visual Block 区域选择,这里的Visual表示视觉,图像,可视化. 2.    小写v:字符选择     shift+v(大写V):行选择               ctrl+v:矩 ...

  10. 使用Bootstrap3和Ladda UI实现的多种按钮“加载中”效果体验

    在线演示 在线演示 大家在开发基于web的网站或者web应用中,常常在AJAX调用的过程中需要提示用户并且展示相关的“加载中”效果,类似的UI设计也非常多,比如,当点击一个按钮后,在它的旁边显示一个 ...