给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{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
结尾无空行
V1:
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef struct node *pnode;
struct node
{
int value;
pnode left,right;
};
void insert(pnode &tree,int value)
{
if(tree == NULL)
{
tree = (pnode)malloc(sizeof(struct node));
tree->value=value;
tree->left=NULL;
tree->right=NULL;
}
else
{
if(tree->value < value)
{
insert(tree->left,value);
}
else
{
insert(tree->right,value);
}
}
}
int check(pnode treeA,pnode treeB)
{
if(treeA == NULL && treeB == NULL)
{
return 1;
}
if(treeA == NULL || treeB == NULL)
{
return 0;
}
if(treeA->value == treeB->value)
{
return check(treeA->left,treeB->left) && check(treeA->right,treeB->right);
}
return 0;
}
int main()
{
// system("chcp 65001");
cin.tie(0);
cout.tie(0);
// freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
int n;
int L,flag=1,t;
while(cin>>n)
{
if(n==0)
{
break;
}
cin>>L;
pnode treeA=NULL;
for(int i=0; i<n; i++)
{
cin>>t;
insert(treeA,t);
}
for(int i=0; i<L; i++)
{
pnode treeB=NULL;
for(int i=0; i<n; i++)
{
cin>>t;
insert(treeB,t);
}
if(check(treeA,treeB))
{
if(flag)
{
flag=0;
cout<< "Yes";
}
else
cout<< endl<< "Yes";
}
else
{
if(flag)
{
flag=0;
cout<< "No";
}
else
cout<< endl<< "No";
}
}
}
return 0;
}
V2:
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
int check(vector<int>a,vector<int>b)
{
if(a.size() != b.size())
{
return 0;
}
if(a==b)
{
return 1;
}
if(a[0] != b[0])
{
return 0;
}
vector<int>al,ar,bl,br;
for(int i=1; i<a.size(); i++)
{
if(a[i] < a[0])
{
al.push_back(a[i]);
}
else
{
ar.push_back(a[i]);
}
}
for(int i=1; i<b.size(); i++)
{
if(b[i] < b[0])
{
bl.push_back(b[i]);
}
else
{
br.push_back(b[i]);
}
}
return check(al,bl) && check(ar,br);
}
int main()
{
// system("chcp 65001");
cin.tie(0);
cout.tie(0);
// freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
int n;
int L,flag=1;
while(cin>>n)
{
if(n==0)
{
break;
}
cin>>L;
vector<int>tree(16);
for(int i=0; i<n; i++)
{
cin>>tree[i];
}
for(int i=0; i<L; i++)
{
vector<int>t(16);
for(int i=0; i<n; i++)
{
cin>>t[i];
}
if(check(tree,t))
{
if(flag)
{
flag=0;
cout<< "Yes";
}
else
cout<< endl<< "Yes";
}
else
{
if(flag)
{
flag=0;
cout<< "No";
}
else
cout<< endl<< "No";
}
}
}
return 0;
}
V3:
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef struct node *pnode;
struct node
{
int value;
pnode left,right;
};
void insert(pnode &tree,int value)
{
if(tree == NULL)
{
tree = (pnode)malloc(sizeof(struct node));
tree->value=value;
tree->left=NULL;
tree->right=NULL;
}
else
{
if(tree->value < value)
{
insert(tree->left,value);
}
else
{
insert(tree->right,value);
}
}
}
int find(pnode treeA,int value,int vis[])
{
if(vis[treeA->value])
{
if(treeA->value < value)
{
return find(treeA->left,value,vis);
}
else if(treeA->value > value)
{
return find(treeA->right,value,vis);
}
return 0;
}
else
{
if(treeA->value == value)
{
vis[value]=1;
return 1;
}
return 0;
}
}
int check(pnode treeA,vector<int>s)
{
int vis[16]= {0};
for(int i=0; i<s.size(); i++)
{
if(!find(treeA,s[i],vis))
{
return 0;
}
}
return 1;
}
int main()
{
// system("chcp 65001");
cin.tie(0);
cout.tie(0);
// freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
int n;
int L,flag=1,t;
while(cin>>n)
{
if(n==0)
{
break;
}
cin>>L;
pnode treeA=NULL;
for(int i=0; i<n; i++)
{
cin>>t;
insert(treeA,t);
}
for(int i=0; i<L; i++)
{
vector<int>s;
for(int i=0; i<n; i++)
{
cin>>t;
s.push_back(t);
}
if(check(treeA,s))
{
if(flag)
{
flag=0;
cout<< "Yes";
}
else
cout<< endl<< "Yes";
}
else
{
if(flag)
{
flag=0;
cout<< "No";
}
else
cout<< endl<< "No";
}
}
}
return 0;
}