樹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.pswp.cn/news/540846.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540846.shtml
英文地址,請注明出處:http://en.pswp.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 …