PAT Search in a Binary Search Tree

Search in a Binary Search Tree

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.

首先 理解题意 给出一个序列  每一个数都是前一个数的左右儿子之一  保证这棵树是搜索树

这道题其实是考察搜索二叉树的建立 我用了个取巧的方法 并没有建二叉树  凡是满足题意的序列 有一个规律

若第二个数比第一个数大 那后面的所有数都比第一个数大  若第三个数比第二个数小 那第三个数以后的数都比第二个数小

以{1,4,2,3}为例子 大家可以验证一下

至于为什么有这个规律 大家想一下搜索二叉树的定义就知道了

下面给出AC代码

 1 #include "stdio.h"
 2 main()
 3 {
 4     int m,n;
 5     scanf("%d%d",&m,&n);
 6     int i,j,k,flag;
 7     int a[100];
 8     for(i=0;i<m;i++)
 9     {
10         flag=0;
11         for(j=0;j<n;j++)
12         {
13             scanf("%d",&a[j]);
14         }
15         for(k=0;k<n-1;k++)
16         {
17             if(a[k+1]>a[k])
18             {
19                 for(j=k+1;j<n;j++)
20                 {
21                     if(a[j]<=a[k])
22                     {
23                         flag=-1;break;
24                     }
25                 }
26             }
27             else
28             {
29                 for(j=k+1;j<n;j++)
30                 {
31                     if(a[j]>=a[k])
32                     {
33                         flag=-1;
34                         break;
35                     }
36                 }
37             }
38         }
39         if(flag==-1)
40             printf("NO\n");
41         else
42             printf("YES\n");
43     }
44
45 }
时间: 2024-10-25 05:09:43

PAT Search in a Binary Search Tree的相关文章

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 作者 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 res

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 作者 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 res

[Leetcode]700. Search in a Binary Search Tree

700. Search in a Binary Search Tree 本题难度: Easy Topic: Binary Tree Description Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted

[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. It provides a dramatic performance boost over searching linearly through a list for an element. Let’s play around number of iterations required for e

PAT 1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a nod

PAT Advanced 1064 Complete Binary Search Tree (30分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate

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 the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. Have you

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 the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. Example

Lintcode11 Search Range in Binary Search Tree solution 题解

[题目描述] Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. 给