04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25)
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;
}
04-树7. Search in a Binary Search Tree (25)的更多相关文章
- 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 作者 ...
- [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. ...
- 【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; ...
- 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 ...
- 【LeetCode】700. Search in a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
- 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 ...
- 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 ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
随机推荐
- Spring MVC @ModelAttribute 详解
1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...
- 传输层TCPUDP 具体解释
1.传输层存在的必要性 因为网络层的分组传输是不可靠的,无法了解数据到达终点的时间,无法了解数据未达终点的状态.因此有必要增强网络层提供服务的服务质量. 2.引入传输层的原因 面向连接的传输服务与面向 ...
- Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7
昨天,我们介绍了EF的新特性和开发计划,如果你还不了解,请移步 Entity Framework7 有哪些不同?现在开发到什么程度了? .今天,我们开学习全功能.NET(Full .NET)下使用EF ...
- Java操作Microsoft Word之jacob
转自: 现在我们一起来看看,用Java如何操作Microsoft Word. jacob,官网是http://danadler.com/jacob 这是一个开源的工具.最新版本1.7 官方 ...
- MySQL year函数
mysql的日期函数,示例如下:
- [Android 新特性] 15项大改进 Android 4.4新特性解析
腾讯数码讯(编译:刘沙) 终于,Android系统迎来了久违的重大更新——Android 4.4 KitKat,并与新旗舰Nexus 5同时问世.那么,新的系统究竟都有怎样的改进.是否值得升级呢,下面 ...
- 创建一个简单的windows服务,每间隔一定时间重复执行批处理文件
创建一个windows服务项目,增加App.config <?xml version="1.0" encoding="utf-8" ?> <c ...
- iOS网络编程解析协议三:JSON数据传输解析
作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式 基于JavaScript的一个子集 易读性略差,编码手写难度大,数据量小 JSON格式取代了XML给网络传输带来了很大的便利, ...
- webuploader解决不能重复上传问题及一些常见问题处理
uploader = WebUploader.create({ swf: 'resources/js/webuploader-0.1.5/Uploader.swf', // 文件接收服务端. serv ...
- Windows Xp不用安装软件管理多个远程桌面连接
一直使用系统默认的Mstsc来进行远程连接,但如果要连接N个远程的话就比较麻烦 之前也找过第三方的管理软件如:mRemoteNG 此软件有优点就不说了,但我在使用此软件时有一个很大的问题,就是如果一个 ...