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 ...
随机推荐
- 使用 Start 屏幕查找 Windows 更新
使用 Start 屏幕查找 Windows 更新 从屏幕右侧边缘扫入,然后点击“搜索”. 如果您正在使用鼠标,请指向屏幕右下角,然后单击“搜索”. 在搜索框内输入 Windows 更新 ...
- 按字母顺序排列的IDC函数列表
http://www.2cto.com/shouce/ida/162.htm 按字母顺序排列的IDC函数列表 下面是函数描述信息中的约定: 'ea' 线性地址 'success' 0表示函数失败:反之 ...
- UVa-Ecological Premium
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 对jQuery的事件绑定的一些思考
jQuery的事件绑定 问题 首先我们看下面的一个非经常见的事件绑定代码: //example $('#dom').click(function(e){ //do something }); $('# ...
- 使用hadoop实现关联商品统计
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40184581 近期几天一直在看hadoop相关的书籍,眼下略微有点感觉,自己就仿 ...
- Linux使用jstat命令查看jvm的GC情况(转)
B. jstack jstack主要用来查看某个Java进程内的线程堆栈信息.语法格式如下: 1 jstack [option] pid 2 jstack [option] executable co ...
- Appium+python自动化16-appium1.6在mac上环境搭建启动ios模拟器上Safari浏览器
前言 在mac上搭建appium踩了不少坑,先是版本低了,启动后无限重启模拟器.后来全部升级最新版本,就稳稳的了. 环境准备: 1.OS版本号10.12 2.xcode版本号8.3.2 3.appiu ...
- HTML+CSS网站开发兵书
<HTML+CSS网站开发兵书> 基本信息 作者: 高洪涛 丛书名: 程序员藏经阁 出版社:电子工业出版社 ISBN:9787121212369 上架时间:2013-8-26 出版日期:2 ...
- [转] 菜鸟手脱VMP,附上脱壳过程和自己写的脚本,可跨平台
转载:http://www.52pojie.cn/thread-467703-1-1.html 工作需要要脱一个VMP壳,我是一个从来没接触过脱壳的人.瞬间那种心情遇到的人应该都知道!没办法硬着头皮找 ...
- java学习笔记15--多线程编程基础2
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...