树1 树的同构_检查树是否同构
树1 树的同构
Problem statement:
问题陈述:
Write a function to detect if two trees are isomorphic. Two trees are called isomorphic if one of them can be obtained from other by a series of flips, i.e. by swapping left and right children of a number of nodes. Any number of nodes at any level can have their children swapped.
编写一个函数来检测两棵树是否同构 。 如果通过一系列翻转(即通过交换多个节点的左右子节点)可以从另一棵树中获得两棵树,则称为同构树。 任何级别的任何数量的节点都可以交换其子级。
Example1:
范例1:
These two trees are isomorphic
这两个树是同构的
Swap left child & right child of 1
交换左孩子和右孩子1
Swap left & right child of 5
交换5个左右孩子
Example 2:
范例2:
Solution:
解:
The conditions which needed to be satisfied are:
需要满足的条件是:
Empty trees are isomorphic
空树是同构的
Roots must be the same
根必须相同
Either left subtree & right subtree of one must be same with the same of other's, or left subtree of one must been same with right subtree of other's & right subtree of one must same with left subtree of other's.
一个的左子树和右子树必须与另一个的左子树相同,或者一个的左子树必须与另一个的右子树相同,并且一个的右子树必须与另一个的左子树相同。
Pre-requisite:
先决条件:
Two Input binary trees (their roots actually), i.e., root1, root2
两个输入二叉树(实际上是其根),即,root1,root2
FUNCTION isIsomorphic(Node *root1,Node *root2)
1. Both are empty then it's isomorphic. //condition-1
IF (!root1 && !root2)
return true;
If particularly exactly one of them is empty,
then they can't be isomorphic.//extension of condition-1
IF(!root1 || !root2)
return false;
2. If root value are different, they can't be isomorphic//condition-2
IF(root1->data!=root2->data)
return false;
3. Check condition-3
Recursively checking subtrees
return ( ( isIsomorphic(root1->left,root2->left) &&
isIsomorphic(root1->right,root2->right) ) ||
(isIsomorphic(root1->right,root2->left) &&
isIsomorphic(root1->left,root2->right)));
END FUNCTION
C++ implementation
C ++实现
#include <bits/stdc++.h>
using namespace std;
// TreeNode node type
class TreeNode{public:
int val; //value
TreeNode *left; //pointer to left child
TreeNode *right; //pointer to right child
};
// creating new node
TreeNode* newnode(int data)
{
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = data;
node->left = NULL;
node->right = NULL;
return(node);
}
// function to check isomorphic trees
bool isIsomorphic(TreeNode *root1,TreeNode *root2)
{if(!root1 && !root2)
return true;
if(!root1 || !root2)
return false;
if(root1->val!=root2->val)
return false;
return ( (isIsomorphic(root1->left,root2->left) &&
isIsomorphic(root1->right,root2->right) )||
(isIsomorphic(root1->right,root2->left) &&
isIsomorphic(root1->left,root2->right)));
}
int main(){cout<<"tree is built as per example-1\n";
TreeNode *root1=newnode(1);
root1->left= newnode(5);
root1->right= newnode(2);
root1->right->right=newnode(3);
root1->left->left=newnode(8);
root1->left->right=newnode(4);
TreeNode *root2=newnode(1);
root2->left= newnode(2);
root2->right= newnode(5);
root2->right->right=newnode(8);
root2->right->left=newnode(4);
root2->left->right=newnode(3);
if(isIsomorphic(root1,root2))
cout<<"They are isomorphic tree\n";
else
cout<<"They are not isomorphic tree\n";
cout<<"tree is built as per example-2\n";
TreeNode *root3=newnode(1);
root3->left= newnode(9);
root3->right= newnode(2);
root3->right->right=newnode(3);
root3->left->left=newnode(8);
root3->left->right=newnode(4);
if(isIsomorphic(root1,root3))
cout<<"They are isomorphic tree\n";
else
cout<<"They are not isomorphic tree\n";
return 0;
}
Output
输出量
tree is built as per example-1
They are isomorphic tree
tree is built as per example-2
They are not isomorphic tree
Explanation with example
举例说明
Let's check the example-1
让我们看一下示例1
Nodes are represented by their respective values for better understanding
节点由各自的值表示,以便更好地理解
In the main we call isIsomorphic(root1,root2) //isIsomorphic(1,1)
------------------------------------------------
isIsomorphic(1,1)
root1 is not NULL
root2 is not NULL
root1->data==root2->data
thus it returns
((isIsomorphic(root1->left,root2->left) && isIsomorphic(root1->right,root2->right)) ||
(isIsomorphic(root1->right,root2->left) && isIsomorphic(root1->left,root2->right)));
i.e.,
((isIsomorphic(5,2) && isIsomorphic(2,5)) ||
(isIsomorphic(2,2) && isIsomorphic(5,5)));
Thus call to isIsomorphic(5,2), isIsomorphic(2,5),
isIsomorphic(2,2), isIsomorphic(5,5)
------------------------------------------------
isIsomorphic(5,2)
root1 is not NULL
root2 is not NULL
root1->data!=root2->data
thus it returns FALSE
------------------------------------------------
isIsomorphic(2,5)
root1 is not NULL
root2 is not NULL
root1->data!=root2->data
thus it returns FALSE
------------------------------------------------
isIsomorphic(2,2)
root1 is not NULL
root2 is not NULL
root1->data==root2->data
thus it returns
((isIsomorphic(root1->left,root2->left) && isIsomorphic(root1->right,root2->right) ) ||
(isIsomorphic(root1->right,root2->left) && isIsomorphic(root1->left,root2->right)));
i.e.,
((isIsomorphic(NULL,NULL) && isIsomorphic(3,3)) ||
(isIsomorphic(3,NULL) && isIsomorphic(NULL,3)));
Thus call to isIsomorphic(NULL,NULL), isIsomorphic(3,3),
isIsomorphic(3,NULL), isIsomorphic(NULL,3)
------------------------------------------------
isIsomorphic(NULL,NULL)
root1 is NULL
root2 is NULL
thus it return TRUE
------------------------------------------------
isIsomorphic(3,3)
root1 is not NULL
root2 is not NULL
root1->data==root2->data
thus it returns
((isIsomorphic(root1->left,root2->left) && isIsomorphic(root1->right,root2->right) ) ||
(isIsomorphic(root1->right,root2->left) && isIsomorphic(root1->left,root2->right)));
i.e.,
((isIsomorphic(NULL,NULL) && (isIsomorphic(NULL,NULL) ||
(isIsomorphic(NULL,NULL) && (isIsomorphic(NULL,NULL)));
Thus call to isIsomorphic(NULL,NULL), (isIsomorphic(NULL,NULL),
(isIsomorphic(NULL,NULL), (isIsomorphic(NULL,NULL))
All (isIsomorphic(NULL,NULL) returns TRUE
Thus, isIsomorphic(3,3) returns TRUE
------------------------------------------------
isIsomorphic(3,NULL)
exactly one root is empty
Thus it returns FALSE
------------------------------------------------
isIsomorphic(NULL,3)
exactly one root is empty
Thus it returns FALSE
------------------------------------------------
Thus ,
isIsomorphic(2,2)
=((isIsomorphic(NULL,NULL) && isIsomorphic(3,3) ) ||
(isIsomorphic(3,NULL) && isIsomorphic(NULL,3)))
=(TRUE && TRUE)|| (FALSE && FALSE)
=TRUE && FLASE
=TRUE
Same way, we can check that isIsomorphic(5,5) returns TRUE
------------------------------------------------
At main:
isIsomorphic(1,1)
=((isIsomorphic(5,2) && isIsomorphic(2,5)) ||
(isIsomorphic(2,2) && isIsomorphic(5,5)))
=((FALSE && FALSE)||(TRUE && TRUE)
=(FALSE && TRUE)
TRUE
Thus these two trees are isomorphic.
You can check the second example same way & can find returning FALSE
您可以以相同的方式查看第二个示例并找到返回的FALSE
翻译自: https://www.includehelp.com/icp/check-if-tree-is-isomorphic.aspx
树1 树的同构
树1 树的同构_检查树是否同构相关推荐
- 智慧树mysql章节答案免费_智慧树_初识数据库—Mysql_完整免费答案
智慧树_初识数据库-Mysql_完整免费答案 更多相关问题 [单选题]以下各类人群中,蛋白质营养状况处于负氮平衡的是 [单选题]- Tom, why were you _____ from schoo ...
- 智慧树omg期末测试答案_智慧树答案广告文案写作单元期末见面课知到章节测试答案...
智慧树答案广告文案写作单元期末见面课知到章节测试答案 见面课:品牌调性与用户痛点洞察1.问题:洞察的本义是:看穿,观察得很透彻,发现事物内在的内容或意义.选项:A:对B:错答案:[对] 2.问题:洞察 ...
- 智慧树omg期末测试答案_智慧树求职omg第十五章节测试答案
智慧树求职omg第十五章节测试答案 更多相关问题 HTML文档对象模型的根节点是什么节点? [判断题]时代精神是民族精神的生长根基和发展动力 [判断题]刚 体 作 瞬 时 平 动 时, 刚体 上 各 ...
- 智慧树大数据分析python答案_智慧树大数据分析的python基础答案
当前位置:主页 > 娱乐 > 正文 智慧树大数据分析的python基础答案
- 树状选择框测试用例_分类树测试用例设计工具:CTE XL
1.1工具安装 CTE XL是一款免费的分类树测试用例设计工具,安装过程很简单,下载好安装程序后,运行安装程序:如图 点击下一步: 点击下一步: 是否创建桌面按钮: 安装: 安装完成后,需要注册才能获 ...
- 智慧树mysql章节答案免费_智慧树初识数据库—Mysql完整免费答案
智慧树初识数据库-Mysql完整免费答案 更多相关问题 [多选] 微机线路保护装置的纵联保护的通道可以是(). [多选] 分层式结构的变电站综合自动化系统中,管理层由一台或多台微机组成,其具体功能一般 ...
- c++中stringstream_文史哲与艺术中的数学_智慧树章节答案
文史哲与艺术中的数学_智慧树章节答案更多相关问题 His mother told me that he ______ read quite well at the age of five. A) sh ...
- 袋装决策树_袋装树是每个数据科学家需要的机器学习算法
袋装决策树 袋装树木介绍 (Introduction to Bagged Trees) Without diving into the specifics just yet, it's importa ...
- 如何在vs中创建r树索引代码_线段树详解与实现
此篇文章用于记录<玩转数据结构>课程的学习笔记 什么是线段树 线段树也被称为区间树,英文名为Segment Tree或者Interval tree,是一种高级的数据结构.这种数据结构更多出 ...
最新文章
- 《虚拟化与云计算》读书感(三)数据中心的概述
- Django视图之类视图与中间件
- R语言基于Bagging算法(融合多个决策树)构建集成学习Bagging分类模型、并评估模型在测试集和训练集上的分类效果(accuray、F1、偏差Deviance):Bagging算法与随机森林对比
- Python scrapy 命令行传参 以及发送post请求payload参数
- 为LUKS加密的磁盘/分区做增量备份
- 【csust】最小素因子问题(树状数组)
- 《计算机算法设计与分析》题目汇总
- 3蛋白wb_WB常见问题原因分析及解决办法
- 监控mysql的pr_zabbix之监控MySQL
- 使用iftop监控网卡实时流量
- LCD1602简易驱动程序
- RGB网页颜色在线取色器
- HTML基础学习(菜鸟教程和W3school参考手册)
- 工地泥浆流出大量邵阳抽泥浆罐车清理路面泥巴
- datedif函数(datedif函数在哪里找)
- css:table-cell的妙用
- php 数组课件,php学习 数组课件第1/2页
- Nginx网站服务与LNMP架构部署(详解)
- python两个表格相同数据筛选_python如何统计所有文本文件的行数 如何用python实现两个文件重复数据筛选并统计...
- 实时操作系统-与QNX比较-qnx系统优势-qnx性能分析-qnx系统性能分析
热门文章
- javaBean和jsp应用
- make命令及makefile
- itunes未能连接到iphone_iTunes下载_苹果iTunes官方下载「32位|64位」
- Java线程怎么发送消息_Java客户端Socket如何能在阻塞线程下收到服务端发送来的消息?...
- android 保活方案_Android 后台保活手段总结 (上篇)
- delphi 生成 超大量xml_用OpenCV4实现图像的超分别率
- 联想服务器重装2008,联想ThinkSystem机器安装2008R2详细教程
- mysql存储map数据结构_map数据结构
- Nginx的11个执行流程
- ubuntu7.10下的vi用的怪怪的