当前位置:网站首页>Whether the same binary search tree (25 points)
Whether the same binary search tree (25 points)
2022-04-23 08:41:00 【Huaihua second affectionate】
Given an insertion sequence, a binary search tree can be uniquely determined . However , A given binary search tree can be obtained from a variety of different insertion sequences . For example, in sequence {2, 1, 3} and {2, 3, 1} Insert a binary search tree that is initially empty , All get the same result . So for the input of various insertion sequences , You need to determine if they can generate the same binary search tree .
Input format :
The input contains several sets of test data . The... Of each group of data 1 Line gives two positive integers N (≤10) and L, They are the number of inserted elements in each sequence and the number of sequences to be checked . The first 2 Line is given N Positive integers separated by spaces , As the initial insertion sequence . And then L That's ok , Each line gives N An inserted element , Belong to L A sequence to check .
Simplicity , We guarantee that every insertion sequence is 1 To N An arrangement of . When read N by 0 when , Mark end of input , Don't process this data .
Output format :
For each set of sequences that need to be checked , If the binary search tree generated by it is the same as that generated by the corresponding initial sequence , Output “Yes”, Otherwise 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;
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";
}
}
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222766.html
边栏推荐
猜你喜欢

在sqli-liabs学习SQL注入之旅(第十一关~第二十关)

Consensus Token:web3.0生态流量的超级入口

xctf刷题小记
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Failed to convert a NumPy array to a Tensor(Unsupported Object type int)

ESP32程序下载失败,提示超时

1099 establish binary search tree (30 points)

深度学习框架中的自动微分及高阶导数

Failed to convert a NumPy array to a Tensor(Unsupported Object type int)

洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
随机推荐
ajax防止缓存方法
How to generate assembly file
Excle plus watermark
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Misunderstanding of flush () method of OutputStream class
tsdf +mvs
Add listening event to input element
什么是RPC
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
pdf加水印
JVM工具之Arthas使用
Redis Desktop Manager for Mac(Redis可视化工具)
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
OneFlow学习笔记:从Functor到OpExprInterpreter
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
面了一圈,整理了这套面试题。。
00后最关注的职业:公务员排第二,第一是?
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
【IndexOf】【lastIndexOf】【split】【substring】用法详解