树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,一经查实,立即删除!

相关文章

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

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

请写出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代码质量的。本…

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地址功能介绍支持延迟加载分页支持单独分页器组件使用&…

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

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

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 …

“云”上存储初显规模 如何架构是关键

在安防系统中&#xff0c;存储设备只是给数据提供存储空间&#xff0c;数据存储的意义更多是为了给上层应用提供二次挖掘。目前的智能分析、大数据、图帧等技术都是基于数据存储做的数据挖掘。为了将二次挖掘应用的性能提升到最高&#xff0c;在优化分析算法的同时&#xff0c;…

【干货】分享总结:MySQL数据一致性

0、导读 沃趣科技数据库工程师罗小波为大家全面分析如何保证MySQL的数据一致性。 1、活动总结 罗小波老师从MySQL的崩溃数据恢复安全性、MySQL复制原理及异步&semi sync复制原理、MySQL主从服务器如何保证数据一致性等多方面分析如何保证MySQL的数据一致性。 分享内容满满的…

设置html按钮点击事件无效果,css怎么设置按钮不能点击?

css怎么设置按钮不能点击&#xff1f;下面本篇文章就来给大家介绍一下使用CSS设置按钮不能点击的方法。有一定的参考价值&#xff0c;有需要的朋友可以参考一下&#xff0c;希望对大家有所帮助。想要按钮不能点击可以通过设置按钮点击事件失效来实现&#xff1b;而在CSS中&…

计算机图形学与几何造型导论_计算机图形学导论

计算机图形学与几何造型导论历史 (History) The main forerunner sciences to the development of modern computer graphics were the advances in electrical engineering, electronics, and television that took place during the first half of the twentieth century whe…

android 继承listview,Android listView 继承ListActivity的用法

Android listView 继承ListActivity的用法 在手机中经常有列表方式。如果Activity中只有唯⼀⼀个List(这也是通常的情况)&#xff0c;可以继承ListActivity来实现。我们用两个例子来学习List。List例子⼀&#xff1a;利用Android自带的List格式步骤⼀&#xff1a;Android XML文…

html页面授权码,spring boot 2.0 整合 oauth2 authorization code授权码模式

oauth2 authorization code 大致流程用户打开客户端后&#xff0c;客户端要求用户给予授权。用户同意给予客户端授权。客户端使用授权得到的code&#xff0c;向认证服务器申请token令牌。认证服务器对客户端进行认证以后&#xff0c;确认无误&#xff0c;同意发放令牌。客户端请…