给定两棵树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. node.js环境安装,及连接mongodb测试

    1.node.js环境安装 npm config set python python2.7npm config set msvs_version 2013npm config set registry ...

  2. Winform水印

    本文实例展示了WinForm实现为TextBox设置水印文字功能,非常实用的技巧,分享给大家供大家参考. 关键代码如下 using System; using System.Runtime.Inter ...

  3. JSONP和CORS两种跨域方式的简单介绍和解决方案实例

    随着软件开发分工趋于精细,前后端开发分离成为趋势,前端同事负责前端页面的展示及页面逻辑处理,服务端同事负责业务逻辑处理同时通过API为前端提供数据也为前端提供数据的持久化能力,考虑到前后端同事开发工具 ...

  4. 如何使用vmware-vdiskmanager增加磁盘空间

    VMware Virtual Disk Manager Usage: vmware-vdiskmanager.exe OPTIONS <disk-name> | <mount-poi ...

  5. dvwa第一次接触

    DVWA (Damn Vulnerable Web Application)DVWA是用PHP+Mysql编写的一套用于常规WEB漏洞教学和检测的WEB脆弱性测试程序.包含了SQL注入.XSS.盲注等 ...

  6. [转]./configure,make,make install的作用

    ./configure,make,make install的作用(转) 这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台 ...

  7. TObject、Pointer、Interface的转换

    unit Unit4; ));   ));   ));   //将Obj转为接口   //LInf1 := ITest(Pointer(LObj1));       //无法转换了,丢失了接口信息   ...

  8. cPage分页详细介绍

    asp.net中各种数据控件,datalist.gridview.Repeater等分页是最常用的功能,几乎任何一个B/S项目,无论是系统还是网站都会用到.分页时,读取整个数据,直接绑定到控件,都可以 ...

  9. mybatis实战教程(mybatis in action)之三:实现数据的增删改查

    前面已经讲到用接口的方式编程.这种方式,要注意的一个地方就是.在User.xml  的配置文件中,mapper namespace="com.yihaomen.mybatis.inter.I ...

  10. mysql-bin引起mysql不能启动

    日志提示无法找到mysql-bin.000005这个文件 原因是我把其删除了,而在mysql-bin.index文件中却还有mysql-bin.000005的记录,因此启动失败了 解决: cd /va ...