方法还是固定的建树 然后用vector存放遍历的结果 利用vector可以直接判断是否相等的好处 判断遍历结果是否一样。
#include<vector>
#include<cstdio>
#include<stdlib.h>
using namespace std;
typedef struct node{
struct node* left;
struct node*right;
int data;
}*bintree;
vector<int>bst1;
vector<int>bst2;
bintree insert(bintree bst,int x){
if(!bst){
bst = (bintree)malloc(sizeof(struct node));
bst->data = x;
bst->left = bst->right =NULL;
}else{
if(bst->data<=x) bst->right = insert(bst->right,x);
else if(bst->data>x) bst->left =insert(bst->left,x);
}
return bst;
}
void preorder(bintree bst){
if(bst){
bst1.push_back(bst->data);
preorder(bst->left);
preorder(bst->right);
}
}
void preorder1(bintree bst){
if(bst){
bst2.push_back(bst->data);
preorder1(bst->left);
preorder1(bst->right);
}
}
int main()
{
int n,l,x;
scanf("%d",&n);
while(n!=0){
bintree bst = NULL;
scanf("%d",&l);
for(int i=0;i<n;i++){
scanf("%d",&x);
bst =insert(bst,x);
}
preorder(bst);
for(int i=0;i<l;i++){
bintree b=NULL;
for(int j=0;j<n;j++){
scanf("%d",&x);
b = insert(b,x);
}
preorder1(b);
if(bst1==bst2) printf("Yes\n");
else printf("No\n");
bst2.clear();
}
scanf("%d",&n);
bst1.clear();
}
}