1、两个二叉树是否相等
2、前中遍历创建二叉树
bool Equal(BtNode *pa, BtNode *pb)
{
return (pa == NULL && p == NULL) || (pa != NULL && pb != NULL && pa->data == pb->data && Equal(pa->leftchild,pb->leftchild) && Equal(pa->rigtchild,pb->rigtchild)) ;
}
//或者
bool Equal(BtNode *pa, BtNode *pb)
{
if (pa ==NULL && pb==NULL) //两个空树
{
return true;
}
if (pa == NULL || pb == NULL) //有一个为空,一个不为空
{
return false;
}
if (pa->data == pb->data)
{
return Equal(pa->leftchild, pb->leftchild) && Equal(pa->rightchild, pb->rightchild);
}
else
return false;
}
//前中遍历创建二叉树
int FindIs(char *is,int n,ElemType x)
{
for(int i = 0;i<n;++i)
{
if(is[i] == x)
return i;
}
return -1;
}
BtNode * Create(char *ps,char *is,int n) //前中遍历创建二叉树
{
BtNode *s = NULL;
if (n > 0)
{
s = Buynode();
s->data = ps[0];
int pos = FindIs(is,n,ps[0]);
if (pos == -1) exit(1);
s->leftchild = Create(ps+1,is,pos);
s->rightchild = Create(ps+pos+1,is+pos+1,n-pos-1);
}
return s;
}
BtNode * CreatePI(char *ps,char *is) //前中遍历创建二叉树
{
if(ps == NULL || is == NULL)
{
return NULL;
}
else
{
int n = strlen(ps);
return Create(ps,is,n);
}
}