当前位置:网站首页>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);
}
边栏推荐
- YOLOv7论文部分解读【含自己的理解】
- Yyds dry inventory how to locate browser page crash
- Illegal mix of collations for operation ‘UNION‘(bug记录)
- Old wine in new bottles -- sample analysis of recent apt32 (sea Lotus) organizational attacks
- Oracle database download, installation, use tutorial and problem summary
- Can you tell me whether mindspore supports torchvision Model directly uses the pre trained network, such as vgg16
- 安全基础4 ---正则表达式
- Binarysearch basic binary search
- Oracle数据库下载、安装、使用教程及问题汇总
- Software designer afternoon real topic: 2009-2022
猜你喜欢

Shopping guide for high-end flagship projectors: dangbei X3 pro and dangbei F5 are more immersive!

tiktok手机网络环境怎么设置?tiktok怎么破播放量?

授权无线通信标准

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

Introduction to web security ICMP testing and defense

IP地址的概念

Day7: ordered binary tree (binary search tree)

Legal mix of collations for operation 'Union' (bug record)

高效生成接口文档好方法

Imperial cms7.5 imitation "question and answer library" question and answer learning platform website source code with mobile version
随机推荐
Network data request for wechat applet development
Security foundation 6 - vulnerability recurrence
VMware 虚拟机下载、安装和使用教程
Creative drop-down multi choice JS plug-in download
Univariate function integration_ Partial integral method
Six axis sensor use learning record
Sccm2012r2 network deployment reinstallation system
微信小程序开发之网络数据请求
Code sharing of social chat platform developed by dating website (III)
Introduction to web security ICMP testing and defense
高端旗舰投影仪选购指南:当贝X3 Pro、当贝F5观影更沉浸!
[wp]ctfshow-web introductory information collection
Detailed evaluation of current popular redis visual management tools
国内常见php的CMS建站系统情况分析
sentinel简单限流和降级demo问题记录
从瞳代到“瞳代”再到品牌,暴利的美瞳的变与未变
网络数据包多层传输演示
[CSAPP Practice Problem 2.32] tsub_ok(int x, int y)判断补码减法是否溢出
Add a subtitle of 3D effect to the container
GBASE 8s UDR内存管理_02_mi_dalloc

