数据结构实验之二叉树一:树的同构
来源:互联网 发布:淘宝分销什么意思 编辑:程序博客网 时间:2024/05/21 17:08
数据结构实验之二叉树一:树的同构
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
图1
图2
现给定两棵树,请你判断它们是否是同构的。
Input
输入数据包含多组,每组数据给出2棵二叉树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出”-”。给出的数据间用一个空格分隔。
注意:题目保证每个结点中存储的字母是不同的。
注意:题目保证每个结点中存储的字母是不同的。
Output
如果两棵树是同构的,输出“Yes”,否则输出“No”。
Example Input
8A 1 2B 3 4C 5 -D - -E 6 -G 7 -F - -H - -8G - 4B 7 6F - -A 5 1H - -C 0 -D - -E 2 -
Example Output
Yes
#include <iostream>
using namespace std;
struct Tree
{
char data;
int left,right;
}t1[10],t2[10];
int Creat(Tree t[],int n)
{
int root=-1,i,check[10];
char l,r;
if(n)
{
for(i=0;i<n;i++)
{
check[i]=0;
}
for(i=0;i<n;i++)
{
cin>>t[i].data>>l>>r;
if(l!='-')
{
t[i].left=l-'0';
check[t[i].left]=1;
}
else
{
t[i].left=-1;
}
if(r!='-')
{
t[i].right=r-'0';
check[t[i].right]=1;
}
else
{
t[i].right=-1;
}
}
for(i=0;i<n;i++)
{
if(!check[i])
{
root=i;
break;
}
}
}
return root;
}
int f(int n,int m)
{
if(n==-1 && m==-1)//都是空 ,同构
{
return 1;
}
if((n==-1 && m!=-1)||(n!=-1 && m==-1))//其中一个为空,不同构
{
return 0;
}
if(t1[n].data!=t2[m].data)//根数据不同,不同构
{
return 0;
}
if(t1[n].left==-1 && t2[m].left==-1)//左子树为空,则判断右子树
{
return f(t1[n].right,t2[m].right);
}
if((t1[n].left!=-1)&&(t2[m].left!=-1)&&(t1[t1[n].left].data==t2[t2[m].left].data))//两树左子树皆不空,且值相等
{
return (f(t1[n].left,t2[m].left)&&f(t1[n].right,t2[m].right));//判断其子树
}
else//两树左子树有一个空 或者 皆不空但值不等
{
return(f(t1[n].left,t2[m].right)&&f(t1[n].right,t2[m].left));//交换左右子树判断
}
}
int main()
{
int n,r1,r2;
while(cin>>n)
{
r1=Creat(t1,n);
cin>>n;
r2=Creat(t2,n);
if(f(r1,r2))
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
using namespace std;
struct Tree
{
char data;
int left,right;
}t1[10],t2[10];
int Creat(Tree t[],int n)
{
int root=-1,i,check[10];
char l,r;
if(n)
{
for(i=0;i<n;i++)
{
check[i]=0;
}
for(i=0;i<n;i++)
{
cin>>t[i].data>>l>>r;
if(l!='-')
{
t[i].left=l-'0';
check[t[i].left]=1;
}
else
{
t[i].left=-1;
}
if(r!='-')
{
t[i].right=r-'0';
check[t[i].right]=1;
}
else
{
t[i].right=-1;
}
}
for(i=0;i<n;i++)
{
if(!check[i])
{
root=i;
break;
}
}
}
return root;
}
int f(int n,int m)
{
if(n==-1 && m==-1)//都是空 ,同构
{
return 1;
}
if((n==-1 && m!=-1)||(n!=-1 && m==-1))//其中一个为空,不同构
{
return 0;
}
if(t1[n].data!=t2[m].data)//根数据不同,不同构
{
return 0;
}
if(t1[n].left==-1 && t2[m].left==-1)//左子树为空,则判断右子树
{
return f(t1[n].right,t2[m].right);
}
if((t1[n].left!=-1)&&(t2[m].left!=-1)&&(t1[t1[n].left].data==t2[t2[m].left].data))//两树左子树皆不空,且值相等
{
return (f(t1[n].left,t2[m].left)&&f(t1[n].right,t2[m].right));//判断其子树
}
else//两树左子树有一个空 或者 皆不空但值不等
{
return(f(t1[n].left,t2[m].right)&&f(t1[n].right,t2[m].left));//交换左右子树判断
}
}
int main()
{
int n,r1,r2;
while(cin>>n)
{
r1=Creat(t1,n);
cin>>n;
r2=Creat(t2,n);
if(f(r1,r2))
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
阅读全文
0 0
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- SDUT数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- SDUT3340数据结构实验之二叉树一:树的同构
- SDUT数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- OJ3340数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- SDUT 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 数据结构实验之二叉树一:树的同构
- 解决maven 中bean is not defined问题
- Centos7安装Redis单机版和集群
- 自定义炫酷powershell
- leetcode: 34. Search for a Range
- Spring MVC简单执行流程
- 数据结构实验之二叉树一:树的同构
- Python 常见类型错误
- 5.5
- Getchar函数
- objective,top1 error,top5error
- leetcode: 35. Search Insert Position
- 【Tars】腾讯微服务框架Tars介绍
- Jquery EasyUI使用ComboGrid
- 完美的素数