7-1 树的同构 【已改正】

7-1 树的同构 (20分)

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图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

标准的答案

思路来自浙江大学MOOC

#include <stdio.h>
#include <stdlib.h>

#define MaxTree 10
#define ElementType char
#define Tree int
#define Null -1
struct TreeNode {
	ElementType Element;
	Tree Left;
	Tree Right;
} T1[MaxTree], T2[MaxTree];

Tree BuildTree( struct TreeNode T[] ) {
	int N,i;
	Tree Root=Null;//防止空树时,返回随机值,一定要做合适的初始化啊
	ElementType cl,cr;
	int check[MaxTree];
	scanf("%d\n", &N);
	if (N) {
		for (i=0; i<N; i++)
			check[i] = 0;
		for (i=0; i<N; i++) {
			scanf("%c %c %c\n", &T[i].Element, &cl, &cr);
			if (cl != '-') {
				T[i].Left = cl-'0';
				check[T[i].Left] = 1;
			} else
				T[i].Left = Null;
			/*对cr的对应处理 */
			if (cr != '-') {
				T[i].Right = cr-'0';
				check[T[i].Right] = 1;
			} else
				T[i].Right = Null;
		}
		for (i=0; i<N; i++)
			if (!check[i])
				break;
		Root = i;
	}
	return Root;
}

int Isomorphic ( Tree R1, Tree R2 ) {
	if ( (R1==Null )&& (R2==Null) ) /* both empty */
		return 1;
		
	if ( ((R1==Null)&&(R2!=Null)) || ((R1!=Null)&&(R2==Null)) )
		return 0; /* one of them is empty */
		
	if ( T1[R1].Element != T2[R2].Element )
		return 0; /* roots are different */
		
	if ( ( T1[R1].Left == Null )&&( T2[R2].Left == Null ) )
		/* both have no left subtree */
		return Isomorphic( T1[R1].Right, T2[R2].Right );
		
	if ( ((T1[R1].Left!=Null)&&(T2[R2].Left!=Null))&&
	        ((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)) )
		/* no need to swap the left and the right */
		return ( Isomorphic( T1[R1].Left, T2[R2].Left ) &&
		         Isomorphic( T1[R1].Right, T2[R2].Right ) );
		         
	else /* need to swap the left and the right */
		return ( Isomorphic( T1[R1].Left, T2[R2].Right) &&
		         Isomorphic( T1[R1].Right, T2[R2].Left ) );
}

int main() {
	Tree R1, R2;
	R1 = BuildTree(T1);
	R2 = BuildTree(T2);
	if (Isomorphic(R1, R2))
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}

C++

#include<bits/stdc++.h> 
#include <iostream>
using namespace std;

#define max 10
struct Node {
	char data;
	int left;
	int right;
} t1[max],t2[max];

int check[max];

int Build(struct Node t[]) {
//传入数组,返回根节点
	int n;
	int root=-1;
	int i,j;
	memset(check,0,sizeof(check));
	char cl,cr;//左右孩子
	cin>>n;
	for(i=0; i<n; i++) {
		cin>>t[i].data>>cl>>cr;

		if(cl!='-') {
			t[i].left=cl-'0';
			check[t[i].left]=1;
		} else
			t[i].left=-1;

		if(cr!='-') {
			t[i].right=cr-'0';
			check[t[i].right]=1;
		} else
			t[i].right=-1;
	}

	for(i=0; i<n; i++)
		if(!check[i])
			root=i;
	return root;
}

int Isomorphic(int r1,int r2) {
//传入根节点,返回1或0
	if(r1==-1&&r2==-1)
		return 1;
	else if(r1==-1&&r2!=-1||r1!=-1&&r2==-1)
		return 0;
	else if(t1[r1].data!=t2[r2].data)
		return 0;
	else if(t1[r1].left==-1&&t2[r2].left==-1)
		return Isomorphic(t1[r1].right,t2[r2].right);
	else
		return Isomorphic(t1[r1].left,t2[r2].right)&&Isomorphic(t1[r1].right,t2[r2].left)
		       ||Isomorphic(t1[r1].left,t2[r2].left)&&Isomorphic(t1[r1].right,t2[r2].right);

}

int main() {
	int r1,r2;
	r1=Build(t1);
	r2=Build(t2);
	int x= Isomorphic(r1,r2);
	if(x)
		cout<<"Yes";
	else
		cout<<"No";
	return 0;
}

这个我第一次写的代码【附有详解】

#include <stdio.h>
#include <stdlib.h>

#define MaxTree 10
#define ElementType char
#define Tree int

struct TreeNode {
	ElementType Element;
	Tree Left;
	Tree Right;
} T1[MaxTree],T2[MaxTree];

int check[MaxTree];

Tree BuildTree(struct TreeNode T[]) {
	int N;
	int i;
	int Root=-1;
	char cl,cr;
	scanf("%d\n",&N);
	//判断输入的树是否为空树

	for(i=0; i<N; i++)
		check[i]=0;
	for(i=0; i<N; i++) {
		scanf("%c %c %c\n",&T[i].Element,&cl,&cr);
        
		if(cl!='-') {
			T[i].Left=cl-'0';
			/*
			该节点有左孩子,其左孩子的位置由 T[i].Left记录,故
			T[i].Left所在位置的节点不会是根节点,所以将其置为1.
			最后遍历check数组,没有被置为1的位置,(现在为0)
			就是根节点的位置。
			*/
			check[T[i].Left]=1;
		} else
			T[i].Left=-1;
		if(cr!='-') {
			T[i].Right=cr-'0';
			check[T[i].Right]=1;
		} else
			T[i].Right=-1;
	}

	for(i=0; i<N; i++)
		if(check[i]==0) {
//检查check值为0的节点,这是根节点,没有指向它的节点
            Root=i;
			break;
		}
	
	return Root;//返回根节点
}

int Isomorphic(Tree R1,Tree R2) {
	//这里的R1,R2其实为int类型,当其等于-1时,为空
	if(R1==-1&&R2==-1)
		return 1;
	if(R1==-1&&R2!=-1||R1!=-1&&R2==-1||T1[R1].Element!=T2[R2].Element)
		return 0;
	//两个根节点不相同
	if(T1[R1].Left==-1&&T2[R2].Left==-1)
		return Isomorphic(T1[R1].Right,T2[R2].Right);
	/*下面这种情况:左子树不为空,并且左子树的根节点相同
	R1	表示二叉树 T1 的根节点
	T1[R1].Left		表示 二叉树T1 的左子树根节点在数组中的的位置
	T1[T1[R1].Left].Element  表示 二叉树T1 的左子树根节点的数值
	*/
	if(T1[R1].Left!=-1&&T2[R2].Left!=-1&&T1[T1[R1].Left].Element==T2[T2[R2].Left].Element)
		return Isomorphic(T1[R1].Left,T2[R2].Left)&&Isomorphic(T1[R1].Right,T2[R2].Right);
	else
		return Isomorphic(T1[R1].Left,T2[R2].Right)&&Isomorphic(T1[R1].Right,T2[R2].Left);

}

int main() {
	Tree R1,R2;
	R1=BuildTree(T1);
	R2=BuildTree(T2);
	if(Isomorphic(R1,R2))
		printf("Yes\n");
	else
		printf("No\n");

	return 0;
}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摆烂.MVP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值