是否同一顆二叉樹
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct TreeNode *Tree;
typedef Tree Position;
struct TreeNode
{
ElementType Data;
Tree Left,Right;
int flag;
};
Tree Insert(Tree T, ElementType
Tree MakeTree(int N);
int Judge(Tree T, int N);
int check(Tree T, int Data);
void Reset(Tree T);
void FreeTree(Tree T);
int main()
{
int N,L;
Tree T;
scanf("%d",&N);
while (N) {
T = MakeTree(N);
for (int i=0; i<L; i++) {
if (Judge(T,N)) {
printf("Yes\n");
}
{
printf("No\n");
}
Reset(T);
}
FreeTree(T);
scanf("%d",&N);
}
return 0;
}
Tree Insert(Tree T, ElementType Data)
{
if (T == NULL) {
T = (Tree)malloc(sizeof(struct TreeNode));
T->Data = Data;
T->Left = T->Right = NULL;
T->flag = 0;
}else if (Data < T->Data) {
T->Left = Insert(T->Left, Data);
}else if(Data > T->Data)
{
T->Right = Insert(T->Right, Data);
}
return T;
}
Tree MakeTree(int N)
{
Tree T = NULL;
int value;
for (int i=0; i<N; i++) {
scanf("%d",&value);
T = Insert(T, value);
}
return T;
}
int Judge(Tree T, int N)
{
int value = 0;
int flag = 0;
scanf("%d",&value);
if (value != T->Data) {
flag = 1;
}else{
T->flag = 1;
}
for (int i=1; i<N; i++) {
scanf("%d",&value);
if ((!flag) && !check(T, value)) {
flag = 1;
}
}
if (flag) {
return 0;
}else
{
return 1;
}
}
int check(Tree T, int Data)
{
if (T->flag) {
if (Data < T->Data) {
return check(T->Left, Data);
}else if(Data > T->Data)
{
return check(T->Right, Data);
}else
{
return 0;
}
}else
{
if (Data == T->Data) {
T->flag = 1;
return 1;
}else
{
return 0;
}
}
}
void Reset(Tree T)
{
if (T->Left) {
Reset(T->Left);
}
if (T->Right) {
Reset(T->Right);
}
T->flag = 0;
}
void FreeTree(Tree T)
{
if (T->Left) {
FreeTree(T->Left);
}
if (T->Right) {
FreeTree(T->Right);
}
free(T);
}