判断二叉树是否相等

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

typedef struct node
{
int data;
struct node *lc;
struct node *rc;
}BiNode,*BiTree;

void create(BiTree &T)
{
int x;
scanf("%d",&x);
if(x==0) T=NULL;//在运行时别忘了,输入0
else
{
T=(BiNode *)malloc(sizeof(BiNode));
T->data=x;
create(T->lc);
create(T->rc);
}
}

int equal(BiTree T1,BiTree T2)
{
if(T1==NULL&&T2==NULL)
return 1;
if(T1!=NULL&&T2==NULL)
return 0;
if(T2!=NULL&&T1==NULL)
return 0;
if(T1->data==T2->data)
{
if(equal(T1->lc,T2->lc)&&equal(T1->rc,T2->rc))
return 1;
}
return 0;
}

int main()
{
BiTree T1,T2;
printf("输入一个字符串:\n");
create(T1);
printf("输入另一个字符串:\n");
create(T2);
if(equal(T1,T2))
printf("YES\n");
else
printf("NO\n");
return 0;
}

posted on 2016-10-30 19:41  Dove1  阅读(258)  评论(0编辑  收藏  举报