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

图1

图2

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

输入格式:

输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数NNN (≤10\le 10≤10),即该树的结点数(此时假设结点从0到N−1N-1N−1编号);随后NNN行,第iii行对应编号第iii个结点,给出该结点中存储的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

==========================
第一次code:
 #include <stdio.h>
 #include <stdlib.h>

 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 /*
     结构数组表示二叉树:静态链表
 */
 #define MaxTree 100
 #define ElementType char
 #define Tree int
 #define Null -1 

 struct TreeNode
 {
     ElementType Element;
     Tree Left;
     Tree Right;
  } T1[MaxTree],T2[MaxTree];

 Tree BuildTree(struct TreeNode T[]);
 int isSame(Tree R1,Tree R2);
 int main(void)
 {
     Tree R1,R2;
     R1 = BuildTree(T1);
     R2 = BuildTree(T2);
     if(isSame(R1,R2))
     {
         printf("Yes\n");
     }
     else
     {
         printf("No\n");
     }
     ;
 }
 /*
     建立二叉树
 */
 Tree BuildTree(struct TreeNode T[])
 {
     int N,i,Root;
     ElementType cl,cr;
     };
     scanf("%d\n",&N);
     if( N )
     {
         ;i < N;i++)
         {
             check[i]=;
         }
         ;i < N;i++)
         {
             scanf("%c %c %c\n",&T[i].Element,&cl,&cr);
             if(cl != '-')
             {
                 T[i].Left = cl-';
                 check[T[i].Left] = ;
             }
             else
             {
                 T[i].Left = Null;
             }
             if(cr != '-')
             {
                 T[i].Right = cr-';
                 check[T[i].Right] = ;
             }
             else
             {
                 T[i].Right = Null;
             }
         }
         ;i < N;i++)
         {
             if(!check[i])
             {
                 Root = i;
                 break;
             }
         }
     }
     return Root;
 }
 /*
     判断是否同构
 */
 int isSame(Tree R1,Tree R2)
 {
     if((R1 == Null) && (R2 == Null))
     {
         ;
     }
     if( (R1 == Null && R2 != Null) || (R1 != Null && R2 == Null))
     {
         ;
     }
     else
     {
         if(T1[R1].Element != T2[R2].Element)
         {
             ;
         }
         if(T1[R1].Left == Null && T2[R2].Left == Null)
         {
             return isSame(T1[R1].Right,T2[R2].Right);
         }
         if((T1[R1].Left != Null && T2[R2].Left != Null) && (T1[R1].Element != T2[R2].Element))
         {
             return (isSame(T1[R1].Left,T2[R2].Left) && isSame(T1[R1].Right,T2[R2].Right));
         }
         else
         {
             return (isSame(T1[R1].Left,T2[R2].Right) && isSame(T1[R1].Right,T2[R2].Left));
         }
     }
 }
 

PAT 03-树1 树的同构 (25分)的更多相关文章

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

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  2. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

  3. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  4. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  5. PAT 乙级 1080 MOOC期终成绩 (25 分)

    1080 MOOC期终成绩 (25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的 ...

  6. pat 1002 A+B for Polynomials (25 分)

    1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...

  7. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  8. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  9. PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)

    1055 The World's Richest (25 分)   Forbes magazine publishes every year its list of billionaires base ...

  10. PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)

    1047 Student List for Course (25 分)   Zhejiang University has 40,000 students and provides 2,500 cou ...

随机推荐

  1. linux下安装安装pcre-8.32

    linux下安装安装pcre-8.32 ./configure --prefix=/usr/local/pcre 出现以下错误 configure: error: You need a C++ com ...

  2. CentOS上安装软件错误提示:configure: error: no acceptable C compiler found in $PATH

    CentOS上安装软件错误提示:configure: error: no acceptable C compiler found in $PATH 因为是centos linux,默认可以采用yum方 ...

  3. Bash命令积累

    复制本目录下除掉所有的.png文件外的所有其他文件到某个目录下: mv -f !(*.png) ./src/ 除掉所有的.png文件外其余的文件,全部删掉 rm -f !(*.png) 本目录下,不显 ...

  4. DBA-mysql-用户控制

    创建: CREATE USER 'jeffrey'@'localhost'  IDENTIFIED BY 'new_password' PASSWORD EXPIRE; 授权: Grant all o ...

  5. JavaScript中常见的数组操作函数及用法

    JavaScript中常见的数组操作函数及用法 昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下 ...

  6. [PHP] - Laravel - 列表、新增、修改、删除例子

    前言 Laravel默认是自带了CURD的功能,使用路由的Route::resource可以做到. 但真正的项目中,这往往不是我们所需要的.因为一个项目会有比较复杂的计算.验证等功能. 下面是对项目中 ...

  7. 从veth看虚拟网络设备的qdisc

    背景 前段时间在测试docker的网络性能的时候,发现了一个veth的性能问题,后来给docker官方提交了一个PR,参考set tx_queuelen to 0 when create veth d ...

  8. urllib2抓取HTML存入Excel

    通过urllib2抓取HTML网页,然后过滤出包含特定字符的行,并写入Excel文件: # -*- coding: utf-8 -*- import sys #import urllib import ...

  9. pickle序列化

    通过pickle来序列化: # -*- coding: utf-8 -*- import pickle #-------------------序列化--------------------- zoo ...

  10. OAF_文件系列12_实现OAF导出PDF方式TemplateHelper

    ap.paymentrequest.webui PaymentRequestSignCO http://wenku.baidu.com/link?url=ujbT5CHkeC1bAtUn9Nsm_Fg ...