是否同一棵二叉搜索树

该文是关于编程的题目,要求编写程序来判断给定的不同插入序列是否能生成相同的二叉搜索树。输入包含多个测试用例,每个用例有初始插入序列和多个待检查序列,所有序列都是1到N的排列。程序需比较生成的二叉搜索树是否一致,一致则输出Yes,否则输出No。提供了C语言的两种实现方式以及C++的实现。
摘要由CSDN通过智能技术生成

给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。

输入格式:

输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。随后L行,每行给出N个插入的元素,属于L个需要检查的序列。

简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。

输出格式:

对每一组需要检查的序列,如果其生成的二叉搜索树跟对应的初始序列生成的一样,输出“Yes”,否则输出“No”。

输入样例:

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

输出样例:

Yes
No
No

鸣谢青岛大学周强老师补充测试数据!

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

代码

C语言

#include<stdio.h>
#include<stdlib.h>

struct BTree
{
    int Data;
    struct BTree* Left;
    struct BTree* Right;
};

typedef struct BTree* BTr;

BTr CreatBT(BTr BT,int num);

void Judge(BTr T1,BTr T2,int *jud);

int main()
{
    int jud=1;
    int num1,num2,num;
    while(1)
    {
        jud=1;
        BTr T1=NULL;
        scanf("%d %d",&num1,&num2);
        if(!num1)
        {
            break;
        }
        for(int i=0;i<num1;i++)
        {
            scanf("%d",&num);
            T1=CreatBT(T1,num);
        }
        while(num2>0)
        {
            jud=1;
            BTr T2=NULL;
            for(int i=0;i<num1;i++)
            {
                
                scanf("%d",&num);
                T2=CreatBT(T2,num);
            }
            Judge(T1,T2,&jud);
            num2--;
            if(jud)
            {
                printf("Yes\n");
            }
            else
            {
                printf("No\n");
            }
        }
    }
    return 0;
}

BTr CreatBT(BTr BT,int num)
{
    if(!BT)
    {
        BT=(BTr)malloc(sizeof(BTr));
        BT->Data=num;
        BT->Left=BT->Right=NULL;
    }
    else
    {
        if(num>BT->Data)
        {
            BT->Right=CreatBT(BT->Right,num);
        }
        else if(num<BT->Data)
        {
            BT->Left=CreatBT(BT->Left,num);
        }
    }
    return BT;
}

void Judge(BTr T1,BTr T2,int *jud)
{
    if(T1||T2)
    {
        if(!T1||!T2||T1->Data!=T2->Data)
        {
            *jud=0;
            return;
        }
        Judge(T1->Left,T2->Left,jud);
        Judge(T1->Right,T2->Right,jud);
    }
}
//PTA-2023-04-19-002

C语言第二种写法(void类型的CreatBT函数)

#include<stdio.h>
#include<stdlib.h>

struct BTree
{
    int Data;
    struct BTree* Left;
    struct BTree* Right;
};

typedef struct BTree* BTr;

void CreatBT(BTr *BT,int num);

void Judge(BTr T1,BTr T2,int *jud);

int main()
{
    int jud=1;
    int num1,num2,num;
    while(1)
    {
        jud=1;
        BTr T1=NULL;
        scanf("%d %d",&num1,&num2);
        if(!num1)
        {
            break;
        }
        for(int i=0;i<num1;i++)
        {
            scanf("%d",&num);
            CreatBT(&T1,num);
        }
        while(num2>0)
        {
            jud=1;
            BTr T2=NULL;
            for(int i=0;i<num1;i++)
            {
                
                scanf("%d",&num);
                CreatBT(&T2,num);
            }
            Judge(T1,T2,&jud);
            num2--;
            if(jud)
            {
                printf("Yes\n");
            }
            else
            {
                printf("No\n");
            }
        }
    }
    return 0;
}
//传入的参数是指针的指针
void CreatBT(BTr *BT,int num)
{
    if(!*BT)
    {
        *BT=(BTr)malloc(sizeof(BTr));
        (*BT)->Data=num;
        (*BT)->Left=(*BT)->Right=NULL;
    }
    else
    {
        if(num>(*BT)->Data)
        {
            CreatBT(&(*BT)->Right,num);
        }
        else if(num<(*BT)->Data)
        {
            CreatBT(&(*BT)->Left,num);
        }
    }
}

void Judge(BTr T1,BTr T2,int *jud)
{
    if(T1||T2)
    {
        if(!T1||!T2||T1->Data!=T2->Data)
        {
            *jud=0;
            return;
        }
        Judge(T1->Left,T2->Left,jud);
        Judge(T1->Right,T2->Right,jud);
    }
}
//VS2022-2023-04-17-003

C++

#include<iostream>
using namespace std;

struct BTree
{
    int Data;
    struct BTree* Left;
    struct BTree* Right;
    BTree(int num)
    {
        Data = num;
        Left = Right = NULL;
    }
};
//C++的结构体可以写构造函数
typedef BTree* BTr;

BTr CreatBT(BTr BT, int num);

void Judge(BTr T1, BTr T2, bool* jud);

int main()
{
    bool jud = true;
    int num1, num2, num;
    while (1)
    {
        jud = true;
        BTr T1 = NULL;
        cin>>num1>>num2;
        if (!num1)
        {
            break;
        }
        for (int i = 0;i < num1;i++)
        {
            cin>>num;
            T1=CreatBT(T1, num);
        }
        while (num2 > 0)
        {
            jud = 1;
            BTr T2 = NULL;
            for (int i = 0;i < num1;i++)
            {

                cin>>num;
                T2=CreatBT(T2, num);
            }
            Judge(T1, T2, &jud);
            num2--;
            if (jud)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
    }
    return 0;
}

BTr CreatBT(BTr BT, int num)
{
    if (!BT)
    {
        BT = new BTree(num);
    }
    else
    {
        if (num > BT->Data)
        {
            BT->Right=CreatBT(BT->Right, num);
        }
        else if (num < BT->Data)
        {
            BT->Left=CreatBT(BT->Left, num);
        }
    }
    return BT;
}

void Judge(BTr T1, BTr T2, bool* jud)
{
    if (T1 || T2)
    {
        if (!T1 || !T2 || T1->Data != T2->Data)
        {
            *jud = false;
            return;
        }
        Judge(T1->Left, T2->Left, jud);
        Judge(T1->Right, T2->Right, jud);
    }
}
//VS2022-04-19-001

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想换电脑的LoongLy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值