#include<stdio.h> typedefstructTreeNode * Tree; structTreeNode { int v; Tree Left, Right; int flag; }; Tree MakeTree(int N); Tree insert(Tree T, int v); Tree NewNode(int V); intcheck(Tree T, int V); intJudge(Tree T, int N); voidResetT(Tree T); voidFreeTree(Tree T); intmain() { int N, L, i; Tree T; scanf("%d", &N); while (N) { scanf("%d", &L); T = MakeTree(N); for (int i = 0; i < L; i++) { if (Judge(T, N)) printf("Yes\n"); else printf("No\n"); ResetT(T); } FreeTree(T); scanf("%d", &N); } } Tree MakeTree(int N) { Tree T; int i, V;
scanf("%d", &V); T = NewNode(V); for (i = 1; i < N; i++) { scanf("%d", &V); T = insert(T, V); } return T; } Tree insert(Tree T, int V) { if (!T) T = NewNode(V); else { if (V > T->v) T->Right = insert(T->Right, V); else T->Left = insert(T->Left, V); } return T; } Tree NewNode(int V) { Tree T = (Tree)malloc(sizeof(struct TreeNode)); T->v = V; T->Left = T->Right = NULL; T->flag = 0; return T; } intcheck(Tree T, int V) { if (T->flag) { if (V < T->v) return check(T->Left, V); elseif (V > T->v) return check(T->Right, V); else return0; } else { if (V == T->v) { T->flag = 1; return1; } elsereturn0; } } intJudge(Tree T, int N) { int i, V, flag = 0; /* flag: 0代表目前还一致,1代表已经不一致*/ scanf("%d", &V); if (V != T->v) flag = 1; else T->flag = 1; for (i = 1; i < N; i++) { scanf("%d", &V); if ((!flag) && (!check(T, V)))/*这么做是为了把所有的输入都读取完,以免对下一组数据造成影响*/ flag = 1; } if (flag) return0; else return1; } voidResetT(Tree T) { if (T->Left) ResetT(T->Left); if (T->Right) ResetT(T->Right); T->flag = 0; } voidFreeTree(Tree T) { if (T->Left) FreeTree(T->Left); if (T->Right) FreeTree(T->Right); free(T); }