pat04-树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<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)的更多相关文章
- 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 作者 ...
- [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 ...
随机推荐
- (转)C# TextBox ReadOnly / Enabled 时,后台无法取值问题
当页面上的某个TextBox 设置了属性ReadOnly = "True" 或 Enabled = "False" 时,在客户端为其赋值后,在后台代码中却无法获 ...
- 无废话MVC入门教程笔记
自学mvc,看了园子里李林峰写的李林峰写的无废话MVC入门教程笔记,现在有的平时忽略的或是不太清楚的点记下来 1,Html.DropDownList //服务端写法 @{ //下拉列表的值 List& ...
- 微信开放平台 redirect_uri参数错误
微信开放平台 redirect_uri参数错误 请注意是开放平台开放平台,公众平台和开放平台不是同一个. 解决办法 在写 授权回调域 时,地址只用写到域名级,不能写到域名下一级,这和QQ互联的回调 ...
- 记一次IIS应用程序域崩溃的原因
在日常工作中,每次新的功能上线前,我们会搭建一个测试环境提供给客户测试使用,确定无误后才会更新到正式环境上.这一次也不例外,在约定好时间地点,客户进行集中化测试的过程中,反应网站系统打不开,报500错 ...
- 2019.2.10考试T2, 多项式求exp+生成函数
\(\color{#0066ff}{ 题目描述 }\) 为了减小文件大小,这里不写一堆题目背景了. 请写一个程序,输入一个数字N,输出N个点的森林的数量.点有标号. 森林是一种无向图,要求图中不能存在 ...
- luogu1447 [NOI2010]能量采集 莫比乌斯反演
link 冬令营考炸了,我这个菜鸡只好颓废数学题了 NOI2010能量采集 由题意可以写出式子: \(\sum_{i=1}^n\sum_{j=1}^m(2\gcd(i,j)-1)\) \(=2\sum ...
- Educational Codeforces Round 59 (Rated for Div. 2)
熬夜爆肝,智商急剧下降 坐标UTC+8晚上23:35开始 晚上脑袋转的慢,非常慢 T1上来先做还花了好几分钟 T2本来是有式子的我TM写数位DP写炸了然后才发现是有公式 T3英语不好,一开始题意没读懂 ...
- uwsgi01---uwsgi文件
1. 安装 pip install uwsgi //测试uWSGI是否安装成功 在终端中输入以下命令查看uwsgi的版本:uwsgi --version 2.简单运行 运行uwsgi:uwsgi -- ...
- Django forum
Django是比较有名的Python Web框架,很多著名的网站如Instagram就是用的Django.V2EX是一个界面简洁,功能丰富的论坛,最新源码尚未开源.网络上有很多模仿V2EX外观使用其它 ...
- 利用Android studio开发Java工程
1. 新建项目 新建项目肯定是去new,但到底是new project还是new module是一个问题.在这解释一下,如果new project的话是新建一个工程,相当于新建一个工作区,工程中可以有 ...