加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

【数据结构】判断两个二叉树是否相等

发布时间:2020-12-15 06:07:39 所属栏目:安全 来源:网络整理
导读:判断两个二叉树是否相等,就是当根节点相等时两数的左右子树对应相等或互换后相等,应使用递归来解 #include "stdafx.h"#include conio.h#include iostream#include stdio.husing namespace std;typedef struct TreeNode{char data;TreeNode *leftchild;Tree

判断两个二叉树是否相等,就是当根节点相等时两数的左右子树对应相等或互换后相等,应使用递归来解

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <stdio.h>
using namespace std;

typedef struct TreeNode
{
	char data;
	TreeNode *leftchild;
	TreeNode *rightchild;
};

bool CmpTree(TreeNode *Tree1,TreeNode *Tree2)
{
	if (NULL == Tree1 && NULL == Tree2)
	{
		return true;
	}
	else if (NULL != Tree1 && NULL != Tree2)
	{
		return IsTreeCoreEqual(Tree1,Tree2);
	}
	else
	{
		return false;
	}
}

bool IsTreeCoreEqual(TreeNode *Tree1,TreeNode *Tree2)
{
	if (Tree1->data == Tree2->data)
	{
		if (NULL == Tree1->leftchild && NULL == Tree2->leftchild && NULL == Tree1->rightchild && NULL == Tree2->rightchild)
			return true;
		else
			return(IsTreeCoreEqual(Tree1->leftchild,Tree2->leftchild) && IsTreeCoreEqual(Tree1->rightchild,Tree2->rightchild) ||
			IsTreeCoreEqual(Tree1->leftchild,Tree2->rightchild) && IsTreeCoreEqual(Tree1->rightchild,Tree2->leftchild));
	}
	else
	{
		return false;
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读