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. Struts2 include(包含)多个配置文件

    Struts 2自带有“包含文件”功能,包含多个Struts配置文件合并为一个单元. 单个Struts配置文件 让我们来看看一个糟糕的 Struts 2 配置示例. struts.xml <?x ...

  2. 使用hadoop实现关联商品统计

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40184581 近期几天一直在看hadoop相关的书籍,眼下略微有点感觉,自己就仿 ...

  3. asmack xmpp 获取离线消息

    原文:http://plplum.blog.163.com/blog/static/31032400201503015345948/ 注意事项: 1.登录前要将状态设置为离线: ConnectionC ...

  4. Java 枚举常见7种用法

    用法一:常量 在JDK1.5 之前,我们定义常量都是: publicstaticfianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. publ ...

  5. html端编码规范

    理想的方式是让HTML只用于定义内容呈现的结构,让CSS控制内容呈现的样式,而所有功能的实现定义在JavaScript中

  6. css背景颜色渐变

    1.效果 2.代码 /* 基本色 */ background: #3FB0AC; /* chrome 2+, safari 4+; multiple color stops */ background ...

  7. Android -- Sqlite事务

    这也是通过Android Juint来实现的. 基于上一次的工程继续,上一次工程传送门:<Android–Android Juint 与 Sqlite> http://www.cnblog ...

  8. Android -- 压缩与解压文件

    我在做一个项目中,工程文件中有一个功能需要很多图片,图片与app一起打包下来的话有30+M,那么我们就考虑另外下载压缩包,我们将图片取出,工程就只有4+M了,哈哈哈哈,呵呵,真恐怖.那么这样就涉及到另 ...

  9. js 判断页面元素是否存在

    转自:http://www.0fish.com/js/416 原生js写法案例: obj = document.getElementById("someID"); if (obj) ...

  10. Bootstrap3.1开发的响应式个人简历模板

    在线演示 使用bootstrap3快速开发一个响应式的个人简历,如果有兴趣了解如何开发,请访问GB课程库,地址如下: Bootstrap3构建响应式前端设计师简历模板 http://www.gbtag ...