当前位置:网站首页>03-树1 树的同构
03-树1 树的同构
2022-07-25 19:41:00 【疯疯癫癫才自由】
03-树1 树的同构
分数 25
作者 陈越
单位 浙江大学
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
|
|---|
| 图1 |
![]() |
| 图2 |
现给定两棵树,请你判断它们是否是同构的。
输入格式:
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后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
tydedef 是代替已存在的类型,即给已经存在的类型取一个名字;
#define 变量名放中间,末尾无分号;
结点是从0到n-1开始编号,除了根节点的下标没有出现在输入里,其他结点的编号都出现在了输入里,所以用一个hash数组来找出根节点的编号。
coding:
/**
tydedef 是代替已存在的类型,即给已经存在的类型取一个名字;
#define 变量名放中间,末尾无分号;
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef char ElementType;
typedef int Tree;
const int maxn =10;
#define Null -1
struct TNode
{
ElementType data;
Tree left,right;
}T1[maxn],T2[maxn];
bool hashT[maxn]={0};
Tree CreateTree(TNode *T);
bool Issomer(Tree R1,Tree R2);
int main()
{
Tree R1,R2;
R1=CreateTree(T1);
R2=CreateTree(T2);
if(Issomer(R1,R2))
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
//结点是从0到n-1开始编号,除了根节点的下标没有出现在输入里,其他结点的编号都出现在了输入里,
//所以用一个hash数组来找出根节点的编号。
Tree CreateTree(TNode *T) //返回树的头结点
{
Tree R=Null;
int n;
scanf("%d",&n);
getchar();
if(n)
{
memset(hashT,0,sizeof(hashT));
char d,l,r;
for(int i=0;i<n;++i)
{
scanf("%c %c %c",&d,&l,&r);
T[i].data=d;
if(l!='-')
{
int pos=l-'0';
T[i].left=pos;
hashT[pos]=1;
}
else
T[i].left=Null;
if(r!='-')
{
int pos=r-'0';
T[i].right=pos;
hashT[pos]=1;
}
else
T[i].right=Null;
getchar();
}
for(int i=0;i<n;++i)
{
if(hashT[i]==0)
{
R=i;
break;
}
}
}
return R;
}
bool Issomer(Tree R1,Tree R2)
{
if(R1==Null&&R2==Null)
return true; //如果两棵树都是空树
else if((R1==Null&&R2!=Null)||(R1!=Null&&R2==Null))
return false; //如果有一棵树是空树
else if(T1[R1].data!=T2[R2].data)
return false; //如果有两棵树的根节点的值不相等
else if(T1[T1[R1].left].data==T2[T2[R2].left].data) //如果两棵树的左节点相等,就不用交换左右节点
return Issomer(T1[R1].left,T2[R2].left) && Issomer(T1[R1].right,T2[R2].right);
else //交换左右节点
return Issomer(T1[R1].left,T2[R2].right) && Issomer(T1[R1].right,T2[R2].left);
}
边栏推荐
- An idea of solving div adapting to screen
- Network design and planning of a company
- 微信小程序10-微搭模板
- Code sharing of social chat platform developed by dating website (III)
- Research and application of servo driver in robot
- Authorized wireless communication standard
- 【刷题记录】21. 合并两个有序链表
- 给容器添加3d效果的副标题
- 485 current acquisition module dam-8041
- Selenium运行慢 - 通过设置selenium加载策略加快运行速度
猜你喜欢

Heavy! The new book "deep learning in geometry" was released, which was jointly written by imperial Institute of technology /deepmind and other figures ml Daniel. The 160 page PDF expounds the basic p

Software designer afternoon real topic: 2009-2022

滑雪手机端H5小游戏源码下载

Concept of IP address

Wechat applet 10 - wechat template

基于PHP的中非南南合作信息交流平台网站建设

Openresty Lua resty mlcache multi-level cache

C language learning diary 3 - realloc function

How to ensure the consistency of double write between database and cache?

【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
随机推荐
The finished product of wechat campus maintenance and repair applet graduation design (1) development outline
Typeerror: 'STR' object is not callable error reason
Binary tree visualization
Selenium runs slowly - speed up by setting selenium load policy
How to ensure the consistency of double write between database and cache?
Sccm2012r2 network deployment reinstallation system
伺服驱动器在机器人上的研究与应用
高端旗舰投影仪选购指南:当贝X3 Pro、当贝F5观影更沉浸!
NPM semantic version control, solution console prop being mutated: "placement" error
C语言学习日记3——realloc函数
Add a subtitle of 3D effect to the container
Nezha d1-h test microbench
基于海思3559 高效率的 0延时 0拷贝 qt播放器方案
Basic practice of Blue Bridge Cup - shape retrieval of matrix (C language)
Why learn service grid istio
安全基础4 ---正则表达式
哪吒 D1-H 测试 microbench
哈希无向图可视化
Kcon 2022 highlights and agenda revealed!
Heavy! The new book "deep learning in geometry" was released, which was jointly written by imperial Institute of technology /deepmind and other figures ml Daniel. The 160 page PDF expounds the basic p

