PAT 03-树1 树的同构 (25分)
给定两棵树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分)的更多相关文章
- PTA 03-树1 树的同构 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...
- PTA 树的同构 (25分)
PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...
- PAT甲级:1036 Boys vs Girls (25分)
PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 乙级 1080 MOOC期终成绩 (25 分)
1080 MOOC期终成绩 (25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的 ...
- 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 ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- WAMPSERVER PHP版本5.3的降到 5.2?
在这里下载5.2版本PHP,http://sourceforge.net/projects/wampserver/files/WampServer%202%20-%20Extensions/PHP/下 ...
- windows7 阻止copyfile到windows目录的解决办法
一. windows7 x64,uac会阻止copyfile到c:/windows.提示拒绝访问. [会引起uac提示的3种情况: Administrator access token checks. ...
- Flexigrid从对象中加载数据
(有问题,在找…………) Flexigrid是用来动态加载数据的一种比较好(老)的Jquery表插件,然后有些时候,我们需要其从本地或者jQuery对象中加载数据,比如有这么个需求,页面显示中有两个表 ...
- union和union all的区别
UNION 写一篇联合查询(把前后两个表的查询结果集合在前表中)首先有个为什么需要 相同记录数?? 记错了.应该是union两张表的查询字段数目要一致,字段类型要相似相同的数据类型,至少是相似,可转化 ...
- How to backup your blogs on cnblogs
This is an alternative to OfflineExplorer. Thanks for this article[1] which is listed in Reference s ...
- not子查询中有null值的时候 not in 会失效
not in子查询中有null值的时候 not in 会失效 但是 in 的子查询中有null的 不会失效
- MFC ADO连接Sql Server数据库报无效指针的问题
相关症状: Win7sp1上编译的ADO程序无法在低版本系统上运行,创建ADO时提示错误:0x80004002 解决办法如下: 1.下载: http://download.microsoft.c ...
- svn-git
1.创建版本库 # svnadmin create proj 2.修改配置文件 Auth文件 [groups] admin=shguo [/] @admin=rw *= ...
- 本地新建项目提交到github
1.在github上创建项目(可以添加README.md),创建后的地址为 https://github.com/xxx/xxx-demo.git 2.在eclipse上新建个quick-start的 ...
- Linux学习笔记(二)
1.tzselect无法是使用 vim /usr/bin/tzselect 将 ${TZDIR=pwd}改为${TZDIR=/usr/share/zoneinfo} 2.sudo apt-get in ...