给定两棵树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. .net frameworkAPI文档下载地址

    http://www.msdn.hk/html/2014/5.html VS2013 ILdasm 反编译工具安装在下面地址里了 C:\Program Files (x86)\Microsoft SD ...

  2. js post提交

    /* js跳转页面 url跳转地址,params参数列表 */ function Posturl(url,params){ //创建form表单 var temp_form=document.crea ...

  3. 如何判断exe或dll的目标平台及是否是.NET?

    1. COFF文件头中偏移0处的Machine指示目标机器类型(IMAGE_FILE_MACHINE_AMD64等),偏移18处的Characteristics位指示文件属性(IMAGE_FILE_3 ...

  4. 如何区分Babel中的stage-0,stage-1,stage-2以及stage-3(二)

    上一篇文章我们介绍了法力无边的stage-0 和 包罗万象的stage-1, 现在我们来介绍下 stage-2 和 stage-3 深藏不露的stage-2 为什么说 stage-2深藏不露呢,因为它 ...

  5. IntelliIDEA注册码

    [http://idea.lanyus.com/] BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl ...

  6. 【linux】之相关命令

    防火墙 ) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off ) 即时生效,重启后失效 开启: service iptables s ...

  7. IIS7.5下发布应用程序备忘

    近期工作需要把应用程序发布升级到IIS7.5中,IIS7部署方式跟IIS6差别还是挺大的. 部署常规方式:新建虚拟目录>转成应用程序. 在本机用http://localhost/别名访问一切正常 ...

  8. 修改eclipse中包的显示结构为树形

  9. MVC4 +EasyUI Tabs 使用

    Tabs 右键菜单功能实现 前端 <div id="tabs" class="easyui-tabs" fit="true" bord ...

  10. shared_ptr 线程安全

    Then what's really happening is TWO different sections of memory are being allocated. It's done at o ...