SDUT数据结构实验之二叉树一:树的同构

#include<bits/stdc++.h>
using namespace std;
struct tree
{
    char c;
    struct tree *lc,*rc;
};
struct node
{
    char s;
    int l,r;
} a[15];
struct tree * creat(int k)
{
    struct tree *t;
    t=(struct tree *)malloc(sizeof(struct tree));
    t->lc=t->rc=NULL;
    t->c=a[k].s;
    if(a[k].l!=-1)
        t->lc=creat(a[k].l);
    if(a[k].r!=-1)
        t->rc=creat(a[k].r);
    return t;
};
struct tree *build(int n)
{
    int j;
    bool v[15];
    memset(v,false,sizeof(v));
    for(int i=0; i<n; i++)
    {
        char s1[10],s2[10],s3[10];
        scanf("%s%s%s",s1,s2,s3);
        a[i].s=s1[0];
        if(s2[0]=='-')
            a[i].l=-1;
        else
        {
            a[i].l=s2[0]-'0';
            v[a[i].l]=true;
        }
        if(s3[0]=='-')
            a[i].r=-1;
        else
        {
            a[i].r=s3[0]-'0';
            v[a[i].r]=true;
        }
    }

    if(n!=0)
    {
        for(j=0; j<n; j++)
            if(!v[j])
                break;
    }
    struct tree *root1=creat(j);
    return root1;
};
int ju(struct tree *t1,struct tree *t2)
{
    if(t1==NULL&&t2==NULL)
        return 1;
    if(t1!=NULL&&t2!=NULL)
        if(t1->c==t2->c)
            if((ju(t1->lc,t2->lc)&&ju(t1->rc,t2->rc))||(ju(t1->rc,t2->lc)&&ju(t1->lc,t2->rc)))
                return 1;
    return 0;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        struct tree *root1=build(n);
        int m;
        scanf("%d",&m);
        struct tree *root2=build(m);
        if(ju(root1,root2))
            printf("Yes\n");
        else
            printf("No\n");
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值