7-20 Binary Search Tree (25分)

A binary search tree is uniquely determined by a given ordered insertions of a sequence of positive integers. On the other hand, a given binary search tree may correspond to several different insertion sequences. Now given several insertion sequences, it is your job to determine if they can generate the same binary search tree.

Input Specification:

Input consists of several test cases. For each test case, the first line contains two positive integers N (≤10) and L, which are the total number of integers in an insertion sequence and the number of sequences to be tested, respectively. The second line contains N positive integers, separated by a space, which are the initially inserted numbers. Then L lines follow, each contains a sequence of N integers to be checked.

For convenience, it is guaranteed that each insertion sequence is a permutation of integers 1 to N - that is, all the N numbers are distinct and no greater than N. The input ends with N being 0. That case must NOT be processed.

Output Specification:

For each test case, output to the standard output. Print in L lines the checking results: if the sequence given in the i-th line corresponds to the same binary search tree as the initial sequence, output to the i-th line Yes; else output No.

Sample Input:

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Sample Output:

Yes
No
No

用数组建树,模仿字典树的建树过程这样结点都在数组中连续,然后对于测试序列,从根节点开始比较如果当前结点标记过了,则用测试结点与之比较确定往左还是往右比较,如果没标记过,直接比较是否相等。代码:
#include <cstdio>
#include <cstring>
using namespace std;

int trie[11][2],v[11],vis[11],ind;
void insert(int d) {
    if(ind == 0) {
        v[ind ++] = d;
    }
    else {
        int x = 0;
        while(v[x] != d) {
            if(d < v[x]) {
                if(!trie[x][0]) {
                    trie[x][0] = ind;
                    v[ind ++] = d;
                }
                x = trie[x][0];
            }
            else {
                if(!trie[x][1]) {
                    trie[x][1] = ind;
                    v[ind ++] = d;
                }
                x = trie[x][1];
            }
        }
    }
}
bool check(int x,int d) {
    if(!vis[x]) {
        if(v[x] == d) {
            vis[x] = 1;
            return true;
        }
        return false;
    }
    return d < v[x] ? check(trie[x][0],d) : check(trie[x][1],d);
}
int main() {
    int n,l,d,flag;
    while(scanf("%d",&n) && n) {
        scanf("%d",&l);
        ind = 0;
        memset(trie,0,sizeof(trie));
        for(int i = 0;i < n;i ++) {
            scanf("%d",&d);
            insert(d);
        }
        for(int i = 0;i < l;i ++) {
            flag = 1;
            memset(vis,0,sizeof(vis));
            for(int j = 0;j < n;j ++) {
                scanf("%d",&d);
                flag &= check(0,d);
            }
            puts(flag ? "Yes" : "No");
        }
    }
}

原文地址:https://www.cnblogs.com/8023spz/p/12271964.html

时间: 2024-10-08 05:08:03

7-20 Binary Search Tree (25分)的相关文章

1043 Is It a Binary Search Tree (25 分)

1043 Is It a Binary Search Tree (25 分) 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 no

1043 Is It a Binary Search Tree (25分)(树的插入)

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

PAT Advanced 1043 Is It a Binary Search Tree (25分)

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

A1043 Is It a Binary Search Tree (25分)

一.技术总结 这一题是二叉排序树的问题,题目主要是给出二叉排序树的先序遍历或者二叉排序树镜像的先序遍历或其他,如果是前两种输出YES,并且输出各自的后序遍历.后者直接输出NO 关键在于创建树,据我观察发现无论是镜像的先序遍历还是原来二叉排序树的先序遍历,可以直接根据二叉排序树的特点进行树的创建.用inset函数,创建出来都是二叉排序树,没有镜像与不镜像之分.到了这一步,只需处理如何将这个标准的二叉排序树,变成镜像的先序遍历,其实只要将左右子树递归的顺序换一下即可.详细参考代码. 然后就是将其进行

【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)

题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. trick: for(auto it:post) cout<<it<<((it!=post[n-1])?" ":""); 这样输出会使第0,1数据点格式错误...原因未知 cout<<post[0]; for(int i=1;i<

1043 Is It a Binary Search Tree (25分)

1. 题目 2. 思路 如下图 发现规律,对于最左边来说,后面所有的集合都是先小后大或者先大后小,如果小大交错那么不符合 使用1中规律确定是否为镜像,结合二叉排序树的特点,用递归建立树 输出树的后序遍历 3. 注意点 发现规律比较困难 树的题目一般都要用递归 4. 代码 #include<cstdio> #include<algorithm> #include<vector> using namespace std; #define null NULL struct n

pat1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT 1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 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

1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1043. Is It a Binary Search Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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