04-树7. Search in a Binary Search Tree (25)
来源:互联网 发布:软件怎么root 编辑:程序博客网 时间:2024/05/28 16:19
To search a key in a binary search tree, we start from the root and move all the way down, choosing branches accordingto 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 theright subtree of the root 2, which breaks the rule for a binary search tree. Now given a sequence of keys, you aresupposed 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 41 4 2 32 4 1 33 2 4 1Sample Output:
YESNONO
提交代码
————————————————————
能说,自己没有看懂题目。。。。
其实,这个题目的意思就是说,给出的序列是不是一个搜索序列。
比如,你搜索的时候,肯定是一直在一颗子树上进行的,不可能在两棵树之间进行跳跃。
编程思路是,读取数据之后,将其插入树中,如果,不在子树的子树上进行插入,就是false的。
同时,在编写插入数据的时候,需要将树的root的地址传入,因为自己想通过递归,递归出来false还是true。
传入地址后,这样会改变传入的root的值,这点在刚刚开始的时候,一直没有改变,通过单步调试可以看出没有什么变化的,所以需要。
同时,*运算符和->运算符的优先级的大小,需要加上一个括号。
代码如下。
#include <iostream>#include <stdlib.h>using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */typedef struct BinNode{int data;BinNode *l;BinNode *r;}*BinTree;bool Insert(BinTree *bt, int d){if(!*bt){*bt=(BinTree)malloc(sizeof(struct BinNode));(*bt)->l=(*bt)->r=NULL;(*bt)->data=d;}else{if(d<(*bt)->data && (*bt)->r==NULL){if(!Insert(&((*bt)->l),d))return false;}else if(d>(*bt)->data&& (*bt)->l==NULL){if(!Insert(&((*bt)->r),d))return false;}else{return false;}}return true;}int main(int argc, char** argv) {int N=0, M=0;cin>>N>>M;for(int i=0; i<N; i++){int j=0;BinTree root=NULL;for(j=0; j<M; j++){int tmp=0;cin>>tmp;if(!Insert(&root,tmp)){printf("NO\n");break;}}if(j==M) printf("YES\n");}return 0;}
- 04-树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)
- 04-树4. Search in a Binary Search Tree (25)
- PAT 数据结构 04-树7. Search in a Binary Search Tree (25)
- 04-树4. Search in a Binary Search Tree
- Insert Node in a Binary Search Tree
- Insert Node in a Binary Search Tree
- Search Range in Binary Search Tree
- [刷题]Search Range in Binary Search Tree
- lintcode:Search Range in Binary Search Tree
- Search Range in Binary Search Tree
- 递归-----Search Range in Binary Search Tree
- lintcode: Search Range in Binary Search Tree
- Lintcode_11 Search Range in Binary Search Tree
- #11 Search Range in Binary Search Tree
- Search Range in Binary Search Tree
- Search Range in Binary Search Tree
- 数据结构算法代码实现——循环链表、双向链表(五)
- 从子类中筛选出不符合基类的项
- oracle 体系结构详解
- 集群监控指标(一)
- EXTjs 提交的方式
- 04-树7. Search in a Binary Search Tree (25)
- 介绍几个在线画流程图的工具
- VC 仿QQ窗口靠边自动收缩隐藏效果
- 设计模式C++实现-解释器模式
- 《MYCAT权威指南》与MYCAT开放电商项目
- Android后台发送短信
- Struts2+ExtJS+Ajax 登录操作例子
- FIRST BLOOD
- [leetcode] Remove Duplicates from Sorted Array