树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:

 Isomorphic tree example 1

These two trees are isomorphic

这两个树是同构的

Swap left child & right child of 1

交换左孩子和右孩子1

 Isomorphic tree example 2

Swap left & right child of 5

交换5个左右孩子

 Isomorphic tree example 3

Example 2:

范例2:

 Isomorphic tree example 4

Solution:

解:

The conditions which needed to be satisfied are:

需要满足的条件是:

  1. Empty trees are isomorphic

    空树是同构的

  2. Roots must be the same

    根必须相同

  3. 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 树的同构

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540846.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《SEO的艺术(原书第2版)》——第1章 搜索:反映认知、连接商务

第1章 搜索&#xff1a;反映认知、连接商务 搜索已经与当今的社会融为一体。截至2011年8月&#xff0c;全球每个月执行的搜索超过了1580亿次&#xff0c;每天大约执行52亿次。这意味着&#xff0c;每秒平均要执行大约61 000次搜索。此外&#xff0c;用户对搜索查询返回的期望时…

android 动态contextmenu,在Android中使用ContextMenu与ListView

要从选定的ListView项中获取该项&#xff0c;请参考ContextMenuInfo对象(请参见下面的最后一个实现方法)。完整解决方案如下&#xff1a;1)在ListActivity类中为上下文菜单注册ListViewOverridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstance…

《Android应用开发攻略》——2.2 异常处理

2.2 异常处理 Ian Darwin2.2.1 问题Java有一个精心定义的异常处理机制&#xff0c;但是需要花费一定的时间学习&#xff0c;才能高效地使用而不至于使用户或者技术支持人员感到沮丧。2.2.2 解决方案Java提供了一个Exception层次结构&#xff0c;正确地使用它能够带来相当大的灵…

android 默认浏览器 视频播放 二维码,Android调用系统默认浏览器访问的方法

一、启动android默认浏览器这样子&#xff0c;android就可以调用起手机默认的浏览器访问。二、指定相应的浏览器访问1、指定android自带的浏览器访问( “com.android.browser”&#xff1a;packagename &#xff1b;“com.android.browser.BrowserActivity”&#xff1a;启动主…

请写出3个Android布局,一起撸一波干货集中营练练手Android(三)布局+实现篇

MPGankIO 布局篇整个App到了这里就开始准备开始实现逻辑啦&#xff0c;有没有点小期待后续如果有需要可以爬一波包包通缉令的数据O(∩_∩)O~~我们的布局采用5.0之后的新布局控件1.CardViewCardView特别的属性如下&#xff1a;android:cardCornerRadius&#xff1a;在布局中设置…

小米净水器压力传感器_净水器中RO的完整形式是什么?

小米净水器压力传感器RO&#xff1a;反渗透 (RO: Reverse Osmosis) RO is an abbreviation of Reverse Osmosis. It is a course of action that aids the RO water purifier to banish unfavorable ions, dissolved solids, and TDS from the water. Reverse osmosis is the c…

即时通讯应用战争开打,到底谁能最终定义我们的交流方式?

题图&#xff1a;风靡亚洲的Line 北京时间4月4日消息&#xff0c;据科技网站TechRadar报道&#xff0c;对业界来说&#xff0c;即时通讯应用是一个巨大的市场&#xff0c;除了专门发力该领域的公司&#xff0c;专注搜索的谷歌和专注社交的Facebook最近几年也都开始深耕此类应用…

离散点自动生成等高线_有限自动机| 离散数学

离散点自动生成等高线有限状态机 (Finite state machine) A finite state machine (FSM) is similar to a finite state automation (FSA) except that the finite state machine "prints" an output using an output alphabet distinct from the input alphabet. Th…

android点击加号,Android仿微信朋友圈点击加号添加图片功能

本文为大家分享了类似微信朋友圈&#xff0c;点击号图片&#xff0c;可以加图片功能&#xff0c;供大家参考&#xff0c;具体内容如下xml:xmlns:app"http://schemas.android.com/apk/res-auto"android:layout_width"match_parent"android:layout_height&qu…

AI 创业公司 Kyndi 获850万美元融资,帮助公司预测未来

雷锋网(公众号&#xff1a;雷锋网)8月10日消息&#xff0c;据外媒报道&#xff0c; Kyndi 是一家总部位于帕洛阿尔托的 AI 创业公司。该公司今天宣布&#xff0c;已经完成了850万美元的 B 轮融资。 本轮融资的资金来源包括 PivotNorth Capital&#xff0c;Darling Ventures 和 …

css max-width_CSS中的max-width属性

css max-widthCSS | 最大宽度属性 (CSS | max-width property) The max-width property is used to help in setting the width of an element to the maximum. Although if the element or content is already larger than the maximum width then the height of that content…

20个编写现代CSS代码的建议

本文翻译自Danny Markov 的20-Tips-For-Writing-Modern-CSS一文。 本文归纳于笔者的Web Frontend Introduction And Best Practices:前端入门与最佳实践中CSS入门与最佳实践系列&#xff0c;其他的关于CSS样式指南的还有提升你的CSS姿势、Facebook里是怎样提升CSS代码质量的。本…

android package.xml,Android自动化编译设置AndroidManifest.xml中package值(包名)

手动修改Android的AndroidManifest.xml中package值(包名)很简单&#xff0c;手动修改即可。但是项目中需要把Android的项目源代码放到服务器端在客户下载时候动态编译生成&#xff0c;且生成的app签名相同但包名不同(若此时包名相同就是相同的app)&#xff0c;这种需求需要在服…

css 相同的css属性_CSS中的order属性

css 相同的css属性CSS | 订单属性 (CSS | order Property) Introduction: 介绍&#xff1a; Web development is an ever-growing field that would never find its end, therefore it is equally necessary to learn new ways to deal with the elements of the web page or …

StoreServ的ASIC架构师必须面向未来做出决断

StoreServ阵列采用特殊硬件&#xff0c;即一套ASIC来加速存储阵列操作&#xff0c;而且其每代阵列都会在这方面进行重新设计。目前的设计为第五代。 作为惠普企业业务公司研究员兼StoreServ架构师&#xff0c;Siamak Nazari当下主要负责第六代ASIC的设计工作。 每代ASIC设计往往…

android网页省略分页器,Android轻量级网页风格分页器

博客同步自:个人博客主页轻量级仿网页风格分页器&#xff0c;和RecycleView封装一起配合使用&#xff0c;也可单独使用&#xff0c;喜欢就star、fork下吧&#xff5e;谢谢目录功能介绍效果图如何引入简单使用依赖github地址功能介绍支持延迟加载分页支持单独分页器组件使用&…

scala重载无参构造方法_Scala中的无参数方法

scala重载无参构造方法Scala无参数方法 (Scala parameterless method) A method which accepts no parameters from the calling code. It also denotes that there will not be any empty parentheses. These are special types of methods in Scala that are initialized and…

传统存储做到极致也惊人!看宏杉科技发布的CloudSAN

传统存储阵列首先考虑的是高可靠、高性能。那么在成本上、扩展上、部署上就差。 互联网企业带来分布式存储&#xff0c;扩展上、部署上是优势了&#xff0c;但是单节点的可靠性差、数据一致性差、IO延迟大、空间浪费严重&#xff0c;能耗大。 这两者的问题&#xff0c;我想很多…

android inflate,Android 关于inflate

通俗的说,inflate就相当于将一个xml中定义的布局找出来.因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageVie…

keil lic_LIC的完整形式是什么?

keil licLIC&#xff1a;印度人寿保险公司 (LIC: Life Insurance Corporation of India) LIC is an abbreviation of the Life Insurance Corporation of India. It is a public segment insurance and investment group corporation in India that generally deals with life …