数据结构基础—树的同构

hhh 这道题嘛
老师都把代码给出来了

超就好了,也不难理解对吧~ 

03-树1 树的同构(25 分)

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


图1


图2

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

输入格式:

输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (10),即该树的结点数(此时假设结点从0到N1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。

输出格式:

如果两棵树是同构的,输出“Yes”,否则输出“No”。

输入样例1(对应图1):

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 -

输出样例1:

Yes

输入样例2(对应图2):

8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4

输出样例2:

No
作者: 陈越
单位: 浙江大学
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB


//
//  main.c
//  树的同构
//
//  Created by air on 2018/3/27.
//  Copyright © 2018年 air. All rights reserved.
//

#include <stdio.h>
#define Tree int
typedef struct node{
    char element;
    int left;
    int right;
}treeNode;
Tree BuildTree(treeNode T[]);
int Isomorphic(Tree root1, Tree root2);
treeNode T1[15];
treeNode T2[15];

int main(int argc, const char * argv[]) {
    Tree root1,root2;
    root1 = BuildTree(T1);
    root2 = BuildTree(T2);
    if(Isomorphic(root1,root2))
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

int Isomorphic(Tree root1, Tree root2){
    if(root1 == -1 && root2 == -1)
        return 1; /* both empty */
    
    if ((root1 == -1 && root2 != -1)||(root1 != -1 && root2 == -1))
        return 0; /* one of them is empty */
    
    if(T1[root1].element != T2[root2].element)
        return 0; /* boths are different */
    
    if((T1[root1].left == -1)&&(T2[root2].left == -1))
        return Isomorphic(T1[root1].right, T2[root2].right);
                  /* boths have no left subtree */
    if( ((T1[root1].left != -1) &&(T2[root2].left != -1)) && ((T1[T1[root1].left].element) == (T2[T2[root2].left].element)))
        return (Isomorphic(T1[root1].left, T2[root2].left) && Isomorphic(T1[root1].right, T2[root2].right)); /* No need to swap left and the right*/
    else
        return (Isomorphic(T1[root1].right, T2[root2].left) && Isomorphic(T1[root1].left, T2[root2].right));  /* Need to swap left and the right*/
}

Tree BuildTree(treeNode T[]){
    int N,root= -1;
    scanf("%d\n",&N);
    /*read the tree2*/
    int check[15];
    for(int i = 0;i < 15; i++){
        check[i] = 0; /*initialize the check*/
    }
    if(!N)
        return -1;
    if(N){
        for(int i = 0; i < N; i++){
            char b,c;
            scanf("%c %c %c\n",&T[i].element, &b, &c);
            if(b != '-'){
                check[b-'0'] = 1;
                T[i].left = b-'0';
            }else{
                T[i].left = -1;
            }
            if(c != '-'){
                check[c-'0'] = 1;
                T[i].right = c-'0';
            }else{
                T[i].right = -1;
            }
        }
    }
    for (int i = 0; i < N; i++) {
        if(check[i] == 0){
            root = i;
            break;
        }
    }
    return root;
}

```

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值