树的同构

  • 题目描述

  • C语言实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

#define MaxSize 100

typedef char ElementType;

struct TNode
{
	ElementType Elem;
	int Left;
	int Right;
}T1[MaxSize],T2[MaxSize];

int BuildeTree(struct TNode T[])
{
	int N;
	int Root = -1;
	int i = 0;
	char cl, cr;
	int check[MaxSize];
	scanf("%d\n",&N);
	if (N != 0)
	{
		for (i = 0;i < N;i++)
		{
			check[i] = 0;
		}
		for (i = 0;i < N;i++)
		{
			scanf("%c %c %c\n",&T[i].Elem,&cl,&cr);
			if (cl != '-')
			{
				T[i].Left = cl - '0';
				check[T[i].Left] = 1;
			}
			else
			{
				T[i].Left = -1;  //-1表示没有子节点
			}
			if (cr != '-')
			{
				T[i].Right = cr - '0';
				check[T[i].Right] = 1;
			}
			else
			{
				T[i].Right = -1;  //-1表示没有子节点
			}
		}
		for (i = 0; i < N;i++)
		{
			if (!check[i])
			{
				Root = i;
				break;
			}
		}
	}
	return Root;
}

bool Isomorphism(int R1,int R2)
{
	//先对基本情况进行判断
	if (R1 == -1 && R2 == -1)
	{
		return true;  //两颗树都是空树,一定同构
	}
	if ((R1 == -1 && R2 != -1)
		||(R1 != -1 && R2 == -1))
	{
		return false;  //一颗为空树 另一个颗不是空树 一定不同构
	}
	if (T1[R1].Elem != T2[R2].Elem)
	{
		return false;  //两颗树的根节点不相同 必定不同构
	}
	if (T1[R1].Left == -1 && T2[R2].Left == -1)
	{
		Isomorphism(T1[R1].Right, T2[R2].Right);  //左子树为空 则判断其右子树
	}
	if ((T1[R1].Left != -1 && T2[R2].Left != -1) &&
		(T1[T1[R1].Left].Elem == T2[T2[R2].Left].Elem))
	{
		//不用交换左右子树
		return (Isomorphism(T1[R1].Left,T2[R2].Left) && Isomorphism(T1[R1].Right, T2[R2].Right));
	}
	else
	{
		//需要交换左右子树
		return (Isomorphism(T1[R1].Left, T2[R2].Right) && Isomorphism(T1[R1].Right, T2[R2].Left));
	}
}

int main()
{
	int R1, R2;
	R1 = BuildeTree(T1);
	R2 = BuildeTree(T2);
	if (Isomorphism(R1,R2))
	{
		printf("Yes");
	}
	else
	{
		printf("No");
	}
	// system("pause");
	return 0;
}

值得一提的是,这道题目也是用递归的思想来解决。

posted @ 2019-08-28 17:03  尚修能的技术博客  阅读(218)  评论(0编辑  收藏  举报