第三个测试点不能通过,希望有高人指教不足
https://blog.csdn.net/u011386173/article/details/95651268
具体思路参考这篇文章,很详细了
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node* tree;
struct node {
int data;
tree left;
tree right;
int flag;
};
tree insert(tree bt, int x) {
if (!bt) {
bt = new node;
bt->data = x;
bt->left = bt->right = NULL;
bt->flag = 0;
}
else if (x < bt->data)
bt->left = insert(bt->left, x);
else if (x > bt->data)
bt->right = insert(bt->right, x);
return bt;
}
int check(tree bt, int x) {
if (bt->flag) {
if (x < bt->data)
return check(bt->left, x);
else if (x > bt->data)
return check(bt->right, x);
else
return 0; //出现了两个数据一样的结点,不能构成一棵树;
}
else {
if (x == bt->data) {
bt->flag = 1;
return 1;
}
else
return 0;
}
}
int judge(tree bt, int n) {
int i, v, flag = 0;
cin >> v;
if (v != bt->data)flag = 1; //先判断两个树根节点是否相等
else bt->flag = 1;
for (i = 1; i < n; i++) {
cin >> v;
if ((!flag) && (!check(bt, v))) flag = 1; //要把数读取完不然下一次记会有残留
}
if (flag)return 0;
else return 1;
}
void clear(tree bt) { //清除T的标记
if (bt->left)
clear(bt->left);
else if (bt->right)
clear(bt->right);
bt->flag = 0;
}
void freetree(tree bt) { //释放t的空间
if (bt->left)
freetree(bt->left);
else if (bt->right)
freetree(bt->right);
free(bt);
}
int main() {
int n; cin >> n;
int x; int l;
while (n) {
tree T = new node;
T=NULL;
cin >> l;
for (int i = 0; i < n; i++) {
cin >> x;
T = insert(T, x); //构建一棵树
}
for (int i = 0; i < l; i++) {
if (judge(T, n))cout << "Yes" << endl;
else cout << "No" << endl;
clear(T);
}
freetree(T);
cin >> n;
}
return 0;
}