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


提交代码

法一:

如果是二叉查找树,树上除了叶结点,每个节点的左右子树必有一棵空,另一棵不空。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<vector>
 7 using namespace std;
 8 struct node{
 9     int v;
10     node *l,*r;
11     node(){
12         l=r=NULL;
13     }
14 };
15 int main(){
16     //freopen("D:\\INPUT.txt","r",stdin);
17     int n,m;
18     scanf("%d %d",&n,&m);
19     while(n--){
20         queue<int> q;
21         int i,num;
22         node *p,*pp,*h=NULL;
23         for(i=0;i<m;i++){
24             scanf("%d",&num);
25             q.push(num);
26         }
27         h=new node();
28         h->v=q.front();
29         q.pop();
30         for(i=1;i<m;i++){
31             num=q.front();
32             q.pop();
33             p=h;
34             while(p){
35                 pp=p;//pp point to the father of p
36                 if(num>p->v&&p->l==NULL){
37                     p=p->r;
38                 }
39                 else{
40                     if(num<p->v&&p->r==NULL){
41                         p=p->l;
42                     }
43                     else
44                         break;//防止元素相等
45                 }
46             }
47             if(p==NULL){
48                 p=new node();
49                 p->v=num;
50                 if(num>pp->v){
51                     pp->r=p;
52                 }
53                 else{
54                     pp->l=p;
55                 }
56             }
57             else{
58                 break;
59             }
60         }
61         if(i==m){
62             printf("YES\n");
63         }
64         else{
65             printf("NO\n");
66         }
67     }
68     return 0;
69 }

法二:

先按题意建树,得到树后,用最后输入的数进行树的遍历,如果最后经过m次找到匹配的叶结点,说明序列正确;否则,序列错误。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<vector>
 7 using namespace std;
 8 struct node{
 9     int v;
10     node *l,*r;
11     node(){
12         l=r=NULL;
13     }
14 };
15 int main(){
16     //freopen("D:\\INPUT.txt","r",stdin);
17     int n,m;
18     scanf("%d %d",&n,&m);
19     while(n--){
20         queue<int> q;
21         int i,num,wantfind;
22         node *p,*pp,*h;
23         for(i=0;i<m;i++){
24             scanf("%d",&num);
25             q.push(num);
26         }
27         wantfind=num;  //最后一个
28         h=new node();
29         h->v=q.front();
30         q.pop();
31         for(i=1;i<m;i++){ //建树
32             num=q.front();
33             q.pop();
34             p=h;
35             while(p){
36                 pp=p;//pp point to the father of p
37                 if(num>p->v){
38                     p=p->r;
39                 }
40                 else{
41                     p=p->l;
42                 }
43             }
44             p=new node();
45             p->v=num;
46             if(num>pp->v){
47                 pp->r=p;
48             }
49             else{
50                 pp->l=p;
51             }
52         }
53         int count=0;
54         p=h;
55         while(p){
56             if(wantfind>p->v){
57                 p=p->r;
58             }
59             else{
60                 p=p->l;
61             }
62             count++;
63         }
64         if(count==m){
65             printf("YES\n");
66         }
67         else{
68             printf("NO\n");
69         }
70     }
71     return 0;
72 }

转载于:https://www.cnblogs.com/Deribs4/p/4737182.html

pat04-树7. Search in a Binary Search Tree (25)相关推荐

  1. 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 作者 ...

  2. C#LeetCode刷题之#700-二叉搜索树中的搜索(Search in a Binary Search Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4102 访问. 给定二叉搜索树(BST)的根节点和一个值. 你需要 ...

  3. 内表使用Binary Search的限制

    在ABAP内表操作过程,Read Table有以下一种格式: READ TABLE - free_key Syntax ... WITH KEY comp1 = dobj1 comp2 = dobj2 ...

  4. CleanCodeHandbook Chapter 9: Binary Search(48-50)

    Binary Search 文章目录 Binary Search leetcode35. Search Insert Position leetcode153. Find Minimum in Rot ...

  5. 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)

    议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...

  6. 二分查找树性能分析(Binary Search Tree Performance Analysis)

    经典算法系(21)-二分查找树性能分析(Binary Search Tree Performance Analysis)https://www.douban.com/note/221942390/   ...

  7. PTA甲级 1043 Is It a Binary Search Tree (25分) 树的遍历

    强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬 本文由参考于柳神博客写成 柳神的CSDN博客,这个可以搜索文章 柳神的个人博客,这个没有广告,但是不能搜索 还有就是非常非常有用的 算法笔记 全 ...

  8. 【ACM】二叉搜索树(Binary Search Tree /BS Tree) 小结

    动态管理集合的数据结构--二叉搜索树 搜索树是一种可以进行插入,搜索,删除等操作的数据结构,可以用字典或者优先队列. 二叉排序树又称为二叉查找树,他或者为空树,或者是满足如下性质的二叉树. (1)若它 ...

  9. 二叉搜索树(binary search tree)的建立、删除、查找

    由于输入的数据顺序不同,建立的bst会不一样.最坏的情况就是一个链,所以我们引入了平衡二叉树的概念.这里我们先来看binary search tree.(我随笔里面有一些相关知识) 建立(也就是插入) ...

最新文章

  1. [SimpleOJ238]宝藏探寻
  2. 第14章:信息文档与配置管理和知识与流程管理
  3. extern 用法,全局变量与头文件(重复定义)
  4. ORB论文研读与代码实现
  5. PyG图神经网络框架torch-geometric安装
  6. 核函数与径向基函数 (Radial Basis Function 简称 RBF)详解
  7. python(命令行提示符的实现、四位数能组成多少个互不相同重复三位数的实现)
  8. Android中CursorLoader的使用、原理及注意事项
  9. python+opencv人脸识别(用耶鲁大学的Yale人脸库训练cnn)3
  10. 利用爬虫大量抓取网页图片
  11. 结构梁配筋最牛插件_结构分析|结构抗震概念——强柱弱梁
  12. 鸿蒙 usb调试,usb调试助手
  13. 计算机右键管理 该文件夹,文件右键菜单管理方法介绍【图文详解】
  14. 阿里云Centos6数据盘扩容的问题处理
  15. 韦小宝丝绸|如何鉴别香云纱可以用以下六种方法
  16. 如何在线无痕去除图片水印
  17. 纪念碑谷背后的故事:不差钱!8人团队研发十月
  18. 如何扫描远程主机开放的端口?
  19. 在一个笼子里同事养着一些鸡和兔子,你想了解有多少只鸡和兔,主任对你说:我只告诉你鸡和兔的总头数是16和总脚数是40,你能不能自己计算有多少只鸡和多少只兔?
  20. 使用NasNet模型与keras做深度学习训练时报错

热门文章

  1. 【网络安全工程师面试合集】——网络安全基础知识大总结
  2. php fitnesse,Fitnesse+RestFixture:Web 服务回归测试利器
  3. flex中移除由MXML标签添加的侦听
  4. 计算机基础类报刊,全国“xx杯”计算机应用基础类说课大赛优秀作品:图文表混排-制作感恩报刊说课课件...
  5. 最近公共祖先_LeetCode 236. 二叉树的最近公共祖先
  6. radiobuttonlist 后面追加操作按钮_【进口知识】通关无纸化签约及代理报关委托收发操作指南...
  7. hnu 暑期实训之最少钱币数
  8. 力扣 13.罗马数字转整数
  9. 青蛙的约会 数论 拓展欧几里德
  10. 计算机视觉CV中特征点提取SURF算法的学习笔记