zoukankan      html  css  js  c++  java
  • 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

    我的答案
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <unistd.h>
     4 
     5 #define MaxTree 10
     6 #define ElementType char
     7 #define Tree int
     8 #define Null -1
     9 
    10 struct TreeNode {
    11     ElementType Element;
    12     Tree Left;
    13     Tree Right;
    14 } T1[MaxTree], T2[MaxTree];
    15 int N, check[MaxTree];
    16 
    17 Tree BuildTree(struct TreeNode T[]);
    18 int Isomorphic(Tree R1, Tree R2);
    19 
    20 int main()
    21 {
    22     Tree R1, R2;
    23     R1 = BuildTree(T1);
    24     // printf("R1 build done
    ");
    25     R2 = BuildTree(T2);
    26     // printf("R3 build done
    ");
    27     if(Isomorphic(R1, R2))
    28         printf("Yes
    ");
    29     else
    30         printf("No
    ");
    31     return 0;
    32 }
    33 
    34 Tree BuildTree(struct TreeNode T[])
    35 {
    36     char cl, cr;
    37     int i;
    38     Tree Root = Null;
    39     scanf("%d
    ", &N);
    40     if(N) {
    41         for(i=0;i<N;i++)
    42             check[i] = 0;
    43         for(i=0;i<N;i++) {
    44             scanf("%c %c %c
    ", &T[i].Element, &cl, &cr);
    45             if(cl != '-') {
    46                 T[i].Left = cl - '0';
    47                 check[T[i].Left] = 1;
    48             } else
    49                 T[i].Left = Null;
    50             if(cr != '-') {
    51                 T[i].Right = cr - '0';
    52                 check[T[i].Right] = 1;
    53             } else
    54                 T[i].Right = Null;
    55         }
    56         for(i=0;i<N;i++)
    57             if(check[i] != 1) break;
    58         Root = i;
    59     }
    60     return Root;
    61 }
    62 
    63 int Isomorphic(Tree R1,Tree R2)
    64 {
    65     if((R1==Null)&&(R2==Null))      /* both empty */
    66         return 1;
    67     if(((R1==Null)&&(R2!=Null))||((R1!=Null)&&(R2==Null))) /* one of them is empty */
    68         return 0;
    69     if(T1[R1].Element!=T2[R2].Element)  /* roots are different */
    70         return 0;
    71     if((T1[R1].Left==Null)&&(T2[R2].Left==Null))     /*bot have no left subtree */
    72         return Isomorphic(T1[R1].Right, T2[R2].Right);
    73     if(((T1[R1].Left!=Null)&&(T2[R2].Left!=Null))&&
    74         ((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)))
    75         /* no need to swap the left and the right */
    76         return (Isomorphic(T1[R1].Left, T2[R2].Left)&&
    77                 Isomorphic(T1[R1].Right, T2[R2].Right));
    78     else    /* need to swap the left and the right */
    79         return (Isomorphic(T1[R1].Left, T2[R2].Right) &&
    80                 Isomorphic(T1[R1].Right, T2[R2].Left));
    81 }
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    __weak&__block&__unsafe__unretain
    Unable to convert MySQL date/time value to System.DateTime 错误
    做最好的自己,人生十件事(事业,人生,情感)
    52张扑克牌的Suit(花色)和Rank(牌面大小)排序算法
    windows service在服务器上部署时的问题
    "Unable to compile template. Check the Errors list for details" 问题解决办法
    Javascript中的一些自有方法
    winform中的webbrowser里面操作html代码问题
    Javascript,Jquery实现页面图片预加载百分比展现
    【delphi XE 】 Margin属性 转
  • 原文地址:https://www.cnblogs.com/ch122633/p/8744840.html
Copyright © 2011-2022 走看看