7-20 Binary Search Tree

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<bits/stdc++.h>
using namespace std;
typedef struct BSTNode{
    int data;
    struct BSTNode *lchild, *rchild;
}BSTNode, *BiTree;
void BST_insert(BiTree &T, int k) {
    if(T == NULL) {
        T = (BiTree) malloc (sizeof(BSTNode));
        T -> data = k;
        T -> lchild = T -> rchild = NULL;
    }
    else if(k < T -> data) BST_insert(T->lchild, k);
    else BST_insert(T->rchild, k);
}
int n, t, a[20];
bool flag;
void judge(BiTree T1, BiTree T2) {
    if(T1 == NULL && T2 == NULL) return;
    if(!flag) return; 
    else if(T1 == NULL || T2 == NULL) {
        flag = false;
        return;
    }
    if(T1 -> data != T2 -> data) {
        flag = false;
        return;
    }
    judge(T1 -> lchild, T2 -> lchild);
    judge(T1 -> rchild, T2 -> rchild);
}
int main() {
    while (cin >> n)
    {
        if(n == 0) break;
        scanf("%d", &t);
        BiTree T = NULL;
        for(int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            BST_insert(T, a[i]);
        }
        while (t--)
        {
            flag = true;
            BiTree T2 = NULL;
            int x;
            for(int i = 0; i < n; i++) {
                scanf("%d", &x);
                BST_insert(T2, x);
            }
            judge(T, T2);
            if(flag) puts("Yes");
            else puts("No");
        }
        
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值