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

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

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。




图1

 


图2

现给定两棵树,请你判断它们是否是同构的。

Input
  输入数据包含多组,每组数据给出 2 棵二叉树的信息。对于每棵树,首先在一行中给出一个非负整数 N ( 10) ,即该树的结点数(此时假设结点从 0 N−1 编号);随后 N 行,第 i 行对应编号第 i 个结点,给出该结点中存储的 1 个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出 ”-” 。给出的数据间用一个空格分隔。
注意:题目保证每个结点中存储的字母是不同的。
Output
  如果两棵树是同构的,输出“ Yes ”,否则输出“ No ”。
Example Input
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
Example Output
Yes
Hint
测试数据对应图1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
    char c;
    struct node *l,*r;
};
struct node *head1,*head2;
struct node1
{
    char a,b,c;
}t1[20],t2[20];
struct node *creat(struct node1 t[],int ll,struct node *p)
{
    p=new node;
    p->c=t[ll].a;
    p->l=NULL;
    p->r=NULL;
    if(t[ll].b!='-')
    {
        int x=t[ll].b-'0';
        p->l=creat(t,x,p->l);
    }
    if(t[ll].c!='-')
    {
        int x=t[ll].c-'0';
        p->r=creat(t,x,p->r);
    }
    return p;
}
void prin(struct node *p)
{
    if(p)
    {
        printf("%c",p->c);
        prin(p->l);
        prin(p->r);
    }
}
bool tonggou(node *t1,node *t2)
{
    if(t1==NULL && t2==NULL)   //都是空树
        return true;
    if((t1==NULL && t2!=NULL) || (t1!=NULL && t2==NULL))  //一个空,一个不空
        return false;
    if(t1->c!=t2->c) //对应的节点值相等
        return false;
    if((tonggou(t1->l,t2->l)&&tonggou(t1->r,t2->r)) || (tonggou(t1->l,t2->r)&&tonggou(t1->r,t2->l)))//两棵树的左孩子可能对应,也可能不对应
    return true;
    return false;
}

int main()
{
    int n,m,i,j,k;
    while(cin>>n)
    {
    int v1[20],v2[20];
    memset(v1,0,sizeof(v1));
    memset(v2,0,sizeof(v2));
    for(i=0;i<n;i++)
    {
        cin>>t1[i].a>>t1[i].b>>t1[i].c;
        if(t1[i].b!='-'){v1[t1[i].b-'0']++;}
        if(t1[i].c!='-'){v1[t1[i].c-'0']++;}
    }
    cin>>m;
    for(i=0;i<m;i++)
    {
        cin>>t2[i].a>>t2[i].b>>t2[i].c;
        if(t2[i].b!='-'){v2[t2[i].b-'0']++;}
        if(t2[i].c!='-'){v2[t2[i].c-'0']++;}
    }
    if(n==0 && m==0)//wa了好几次
    {
        printf("Yes\n");
        continue;
    }
    for(i=0;i<n;i++)
    {
        if(!v1[i])
        {
            k=i;
        }
    }
    head1=NULL;
    head2=NULL;
    if(n!=0)
    head1=creat(t1,k,head1);
     for(i=0;i<m;i++)
    {
        if(!v2[i])
        {
            k=i;
        }
    }
    if(m!=0)
    head2=creat(t2,k,head2);
//    prin(head1);
//    printf("\n");
//    prin(head2);
//    printf("\n");

    if(tonggou(head1,head2))
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值