当前位置:网站首页>是否同一棵二叉搜索树 (25 分)
是否同一棵二叉搜索树 (25 分)
2022-04-23 08:35:00 【怀化第二深情】
给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{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
#include<bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
typedef Node* Tree;
Tree insert(Tree BT, int x) {
if (!BT) {
BT = new (Node);
BT->data = x;
BT->left = BT->right = NULL;
} else if (x < BT->data)
BT->left = insert(BT->left, x);
else if (x > BT->data)
BT->right = insert(BT->right, x);
return BT;
}
bool judge(Tree a, Tree b) {
if (a == NULL && b == NULL)
return true;
else if ((a != NULL && b == NULL) || (a == NULL && b != NULL))
return false;
else if (a->data == b->data)
return judge(a->left,b->left)&&judge(a->right,b->right);
return false;
}
int main(){
int n,l,k;
while(cin>>n&&n){
cin>>l;
Tree BT=NULL;
int x;
for(int i=0;i<n;i++){
cin>>x;
BT=insert(BT,x);
}
for(int i=0;i<l;i++){
Tree Temp=NULL;
for(int j=0;j<n;j++){
cin>>x;
Temp=insert(Temp,x);
}
if(judge(Temp,BT))cout<<"Yes\n";
else cout<<"No\n";
}
}
}
版权声明
本文为[怀化第二深情]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dege2929512534/article/details/124339281
边栏推荐
- Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
- [C语言] 文件操作《一》
- 引用传递1
- 扣缴义务人
- Large amount of data submitted by form post
- STM32F103ZET6【标准库函数开发】----库函数介绍
- 作文以记之 ~ 二叉树的前序遍历
- RCC introduction of Hal Library
- An example of network communication based on TCP / IP protocol -- file transmission
- Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
猜你喜欢
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
Redis Desktop Manager for Mac(Redis可视化工具)
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
《深度学习》学习笔记(八)
Harbor企业级镜像管理系统实战
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
HAL库的RCC简介
Get the absolute path of the class according to the bytecode
php基于哈希算法出现的强弱比较漏洞
K210学习笔记(二) K210与STM32进行串口通信
随机推荐
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
K210学习笔记(二) K210与STM32进行串口通信
对OutputStream类的flush()方法的误解
【路科V0】验证环境2——验证环境组件
Excle plus watermark
How to generate assembly file
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
关于数组复制问题
完全二叉搜索树 (30 分)
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
LINQ学习系列-----1.4 匿名对象
Protobuf简介
How to encrypt devices under the interconnection of all things
jsp页面编码
Input / output system
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
Consensus Token:web3.0生态流量的超级入口
HAL库的RCC简介
项目上传部分