数据结构实验之二叉树一:树的同构 (SDUT 3340)
2024-09-21 03:40:58
题解:把原本结构体的左右子树的类型定义成 int 型,用来存放这个结点的左右子树的编号,分别建造两棵二叉树,按个比较,如果在第二棵树中没有找到,那么就不用在判断了。
#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
int l,r;
};
struct node t1[20],t2[20];
void build(struct node *t, int n)
{
for(int i = 0; i < n; i ++)
{
char s[55];
scanf("%s", s);
t[i].data = s[0];
scanf("%s", s);
if(s[0] == '-') t[i].l = 100;
else t[i].l = s[0] - '0';
scanf("%s", s);
if(s[0] == '-') t[i].r = 100;
else t[i].r = s[0] - '0';
}
}
int ok(int i, int j)
{
if(t1[t1[i].l].data == t2[t2[j].l].data && t1[t1[i].r].data == t2[t2[j].r].data)
return 1;
else if(t1[t1[i].r].data == t2[t2[j].l].data && t1[t1[i].l].data == t2[t2[j].r].data)
return 1;
else return 0;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
build(t1,n);
scanf("%d",&m);
build(t2,m);
int f = 0,i,j;
for(i = 0; i < n; i ++)
{
for( j = 0; j < m; j ++)
{
if(t1[i].data == t2[j].data)
{
if(ok(i,j)==0){
f = 1;
break;
}
else break;
}
}
if(f)break;
if(j >= m){f = 1;break;}
}
if(f)printf("No\n");
else printf("Yes\n");
}
return 0;
}
最新文章
- 测试--jmeter的使用
- bootstrap-导航、选项卡
- try catch语句在VC下的处理
- 2014 百度之星题解 1002 - Disk Schedule
- Reactor模型
- 我的Hook学习笔记
- LVS集群的体系结构
- 【Android Developers Training】 51. 序言:打印内容
- 51nod 1020 逆序排列 DP
- Jmeter MD5插件
- ATM+购物商城完整版
- 配置ubuntu
- OPNET仿真软件资料合集
- 从Spring到SpringBoot构建WEB MVC核心配置详解
- C#Delegate.Invoke、Delegate.BeginInvoke And Control.Invoke、Control.BeginInvoke
- 逆变(contravariant)与协变(covariant)
- HBase的简单java操作
- 配置python的豆瓣source
- vue发布后的一些问题
- Android学习笔记_46_Android的intent之间Object、List、List<;Object>;和全局变量数据的传递(Parcelable Serializable)