先建一颗模板数,然后对于每一个测试,检测到达该节点的路径上的每一个点是否遍历过,如果遍历过,则相同,否则树的顺序就不一样
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
using namespace std;
typedef struct TreeNode *Tree;
struct TreeNode
{
int v;
Tree right,left;
int flag;
};
Tree Insert(Tree T,int V)
{
if(!T)
{
T=(Tree)malloc(sizeof(struct TreeNode));
T->v=V;
T->left=T->right=NULL;
T->flag=0;
}
else
{
if(V > T->v)
T->right= Insert(T->right,V);
else
T->left= Insert(T->left,V);
}
return T;
}
Tree marktree(int n)
{
Tree t=(Tree)malloc(sizeof(struct TreeNode));
int V;
cin>>V;
t->v=V;
t->left=t->right=NULL;
t->flag=0;
for(int i=1;i<n;i++)
{
scanf("%d",&V);
t=Insert(t,V);
}
return t;
}
int check(Tree T,int V)
{
if(T->flag)
{
if(V > T->v )
return check(T->right,V);
else if(V < T->v )
return check(T->left,V);
else
return 0;
}
else
{
if(T->v == V)
{
T->flag=1;
return 1;
}
else
{
return 0;
}
}
}
int judge(Tree T,int N)
{
int V,flag=0;
cin>>V;
if(V!=T->v)
flag=1;
else T->flag=1;
for(int i=1;i<N;i++)
{
cin>>V;
if(!check(T,V)) flag=1;
}
if(flag) return 0;
else return 1;
}
void reset(Tree T)
{
if(T->right) reset(T->right);
if(T->left) reset(T->left);
T->flag=0;
}
void freetree(Tree T)
{
if(T->right) freetree(T->right);
if(T->left) freetree(T->left);
free(T);
}
int main()
{
int N,L;
Tree T;
cin>>N;
while(N)
{
cin>>L;
T=marktree(N);
for(int i=0;i<L;i++)
{
if(judge(T,N)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
reset(T);
}
freetree(T);
cin>>N;
}
}