#include <cstdio>
using namespace std;
struct tree {
int data;
tree *l, *r;
tree() {
l=r=NULL;
}
};
int x;
bool f;
tree *insert(tree *root) {
if(!root) {
root=new tree;
root->data=x;
}
else if(x<root->data)
root->l=insert(root->l);
else root->r=insert(root->r);
return root;
}
void judge(tree *root1, tree *root2) {
if(root1&&root2) {
if(root1->data!=root2->data) {
f=0;
return ;
}
judge(root1->l,root2->l);
judge(root1->r,root2->r);
}
}
int main() {
int n, m;
while(~scanf("%d", &n)) {
if(!n) break;
scanf("%d", &m);
tree *root1=new tree;
scanf("%d", &root1->data);
for(int i=1;i<n;i++) {
scanf("%d", &x);
root1=insert(root1);
}
while(m--) {
f=1;
tree *root2=new tree;
scanf("%d", &root2->data);
for(int i=1;i<n;i++) {
scanf("%d", &x);
root2=insert(root2);
}
judge(root1,root2);
if(f) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
数据结构实验之查找一:二叉排序树
最新推荐文章于 2020-12-02 08:55:19 发布
本文介绍了一个使用C++实现的程序,该程序通过构建二叉搜索树并比较两棵树是否相同来判断输入的数据集是否构成相同的二叉搜索树。文章详细展示了如何插入节点到二叉树中,并提供了一个递归函数用于比较两个二叉树是否结构一致且具有相同的元素。
1520

被折叠的 条评论
为什么被折叠?



