当前位置:网站首页>03 isomorphism of tree 1
03 isomorphism of tree 1
2022-07-25 19:44:00 【Madness makes freedom】
03- Trees 1 The isomorphism of trees
fraction 25
author Chen Yue
Company Zhejiang University
Given two trees T1 and T2. If T1 It can be changed into... By exchanging children several times T2, Then we call the two trees “ isomorphism ” Of . For example, figure 1 The two given trees are isomorphic , Because we put one of the tree nodes A、B、G After the exchange of left and right children , You get another tree . Diagram 2 It's not isomorphic .
|
|---|
| chart 1 |
![]() |
| chart 2 |
Here are two trees , Please judge whether they are isomorphic .
Input format :
Type to give 2 Information about a binary tree . For every tree , First, give a nonnegative integer in a row N (≤10), That is, the number of nodes in the tree ( In this case, we assume that the node is from 0 To N−1 Number ); And then N That's ok , The first i Line corresponding to No i Nodes , Give the 1 English capital letters 、 The number of the left child node 、 The number of the right child node . If the child node is empty , In the corresponding position “-”. The data given is separated by a space . Be careful : Make sure that the letters stored in each node are different .
Output format :
If two trees are isomorphic , Output “Yes”, Otherwise output “No”.
sample input 1( Corresponding graph 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 -
sample output 1:
Yes
sample input 2( Corresponding graph 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
sample output 2:
No
tydedef Is to replace the existing type , That is to give a name to an existing type ;
#define Put the variable name in the middle , No semicolon at the end ;
Node is from 0 To n-1 Numbered starting , Except that the subscript of the root node does not appear in the input , The numbers of other nodes appear in the input , So let's use a hash Array to find the number of the root node .
coding:
/**
tydedef Is to replace the existing type , That is to give a name to an existing type ;
#define Put the variable name in the middle , No semicolon at the end ;
*/
#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;
}
// Node is from 0 To n-1 Numbered starting , Except that the subscript of the root node does not appear in the input , The numbers of other nodes appear in the input ,
// So let's use a hash Array to find the number of the root node .
Tree CreateTree(TNode *T) // Return the head node of the tree
{
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; // If both trees are empty
else if((R1==Null&&R2!=Null)||(R1!=Null&&R2==Null))
return false; // If a tree is empty
else if(T1[R1].data!=T2[R2].data)
return false; // If the values of the root nodes of two trees are not equal
else if(T1[T1[R1].left].data==T2[T2[R2].left].data) // If the left nodes of two trees are equal , There is no need to exchange left and right nodes
return Issomer(T1[R1].left,T2[R2].left) && Issomer(T1[R1].right,T2[R2].right);
else // Switch left and right nodes
return Issomer(T1[R1].left,T2[R2].right) && Issomer(T1[R1].right,T2[R2].left);
}
边栏推荐
- Oracle数据库下载、安装、使用教程及问题汇总
- C language learning diary 3 - realloc function
- Bash does not add single quotes to your string
- Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
- Ml programming skills:
- 数字信息化(先枚举假设,再看是否满足条件)(1089 狼人杀-简单版)
- 国内常见php的CMS建站系统情况分析
- On interface encryption
- Network packet multi-layer transmission demonstration
- VMware 虚拟机下载、安装和使用教程
猜你喜欢

Scala基础【集合01】

JS learning notes 17: DOM query exercise

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

Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report

一元函数积分学_分部积分法

YOLOv7论文部分解读【含自己的理解】

Univariate function integration_ Partial integral method

An idea of solving div adapting to screen

TFIDF examples and explanations
![[mindspore] [read graph data] cannot read mindrecord format graph data](/img/2a/6da73178993f3d0f84c1f6ada17884.png)
[mindspore] [read graph data] cannot read mindrecord format graph data
随机推荐
Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
二叉树可视化
一元函数积分学_分部积分法
YOLOv7论文部分解读【含自己的理解】
Research and application of servo driver in robot
ERROR: role “admin“ cannot be dropped because some objects depend on itDETAIL:
Siemens-PLM-TeamCenter下载、安装、使用教程
英诚医院内部网络规划与设计
什么是唯心主义
高效生成接口文档好方法
Wechat applet 10 - wechat template
阿姆利塔工程学院|用于情感分析的方面术语提取中优化采样的强化主动学习方法
项目中new Promise和async、await中的使用,以及promise.all在项目中的实际应用
[hdlbits questions] Verilog language (3) modules: hierarchy section
【好书推荐】-- 《以太网权威指南》(第2版)
485 current acquisition module dam-8041
微信小程序10-微搭模板
what is qml in qt
919. Complete binary tree inserter
加州大学|用于未指定环境的可行对抗鲁棒强化学习

