#include <iostream>
#include <string>
#define ElemType int
using namespace std;
int flag;
typedef struct BinSortTree
{
struct BinSortTree *left;
struct BinSortTree *right;
ElemType data;
}Node;
Node* CreatBST(Node *root, int x);
void CompareBST(Node *root, Node *p);
int main()
{
int n, m, x;
while(cin >> n)
{
if(!n)
{
break;
}
cin >> m;
Node *root = NULL; /// 初始树
for(int i = 0; i < n; i++)
{
cin >> x;
root = CreatBST(root, x);
}
while(m--)
{
Node *p = NULL;
flag = 0;
for(int i = 0; i < n; i++)
{
cin >> x;
p = CreatBST(p, x);
}
CompareBST(root, p);
if(flag)
{
cout << "No" << endl;
}
else
{
cout << "Yes" << endl;
}
}
}
return 0;
}
Node* CreatBST(Node *root, int x)
{
if(!root)
{
root = new Node;
root->left = NULL;
root->right = NULL;
root->data = x;
}
else
{
if(x > root->data)
{
root->right = CreatBST(root->right, x);
}
else if(x < root->data) /// 二叉排序树只插入树内不存在的结点
{
root->left = CreatBST(root->left, x);
}
}
return root;
}
void CompareBST(Node *root, Node *p)
{
if(root && p) /// 如果两棵树都不为空
{
if(root->data != p->data) /// 如果存在不同数据
{
flag = 1;
return;
}
else
{
CompareBST(root->left, p->left);
CompareBST(root->right, p->right);
}
}
}
二叉排序树
最新推荐文章于 2018-03-03 10:59:30 发布