PTA 树的同构(25分)

不多逼逼,直接上代码

// An highlighted block
#include <iostream>
#include <malloc.h>
#define N 100
typedef struct tnode
{
    char data;
    char lchild;
    char rchild; //存结点编号(从0开始)
}tree;
void read(tree t[],int n);

int judge(tree t1[],tree t2[],int n1,int n2);

tree t1[N],t2[N];
int main()
{
    int n1,n2,k;

    scanf("%d",&n1);
    read(t1,n1);
    scanf("%d",&n2);
    read(t2,n2);

    if(n1 == n2 && n1 == 0)
        printf("Yes");
    else if(judge(t1,t2,n1,n2))
        printf("Yes");
    else
        printf("No");

    return 0;
}


void read(tree t[],int n)
{

    int i,j;
    getchar();
    for(i = 0;i < n;i++)
    {
        scanf("%c %c %c",&t[i].data,&t[i].lchild,&t[i].rchild);
        getchar();
    }

}


int judge(tree t1[],tree t2[],int n1,int n2) //判断两棵树是不是同一棵
{
    int i,j;
    int flag = 0,flag1 = 0,flag2 = 0;
    if(n1 != n2)
        return 0;
    else
    {
        for(i = 0;i < n1;i++)
        {
            for(int j = 0;j < n2;j++)
            {
                if(t1[i].data == t2[j].data)
                {
                    if((t1[i].lchild == '-' && t1[i].lchild == t2[j].lchild) ||
                           (t1[t1[i].lchild-48].data == t2[t2[j].lchild-48].data))
                                        flag1 = 1;
                    if((t1[i].rchild == '-' && t1[i].rchild == t2[j].rchild)
                               || (t1[t1[i].rchild-48].data == t2[t2[j].rchild-48].data))
                                        flag2 = 1;

                  //交换左右子树的情况
                    if((t1[i].lchild == '-' && t1[i].lchild == t2[j].rchild) ||
                           (t1[t1[i].lchild-48].data == t2[t2[j].rchild-48].data))
                                        flag1 = 3;
                    if((t1[i].rchild == '-' && t1[i].rchild == t2[j].lchild)
                               || (t1[t1[i].rchild-48].data == t2[t2[j].lchild-48].data))
                                        flag2 = 4;


                        if((flag1 == 1 && flag2 == 1)||(flag1 == 3 && flag2 == 4))
                            flag++;
  
                }
            }
        }

        if(flag == n1)
            return 1;

    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w y z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值