C++實現(xiàn)LeetCode(100.判斷相同樹)
[LeetCode] 100. Same Tree 判斷相同樹
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input: 1 1
/ \ / \
2 3 2 3[1,2,3], [1,2,3]
Output: true
Example 2:
Input: 1 1
/ \
2 2
[1,2], [1,null,2]Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]Output: false
判斷兩棵樹是否相同和之前的判斷兩棵樹是否對稱都是一樣的原理,利用深度優(yōu)先搜索 DFS 來遞歸。代碼如下:
解法一:
class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { if (!p && !q) return true; if ((p && !q) || (!p && q) || (p->val != q->val)) return false; return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };
這道題還有非遞歸的解法,因為二叉樹的四種遍歷(層序,先序,中序,后序)均有各自的迭代和遞歸的寫法,這里我們先來看先序的迭代寫法,相當(dāng)于同時遍歷兩個數(shù),然后每個節(jié)點都進(jìn)行比較,可參見之間那道 Binary Tree Preorder Traversal,參見代碼如下:
解法二:
class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { stack<TreeNode*> st; st.push(p); st.push(q); while (!st.empty()) { p = st.top(); st.pop(); q = st.top(); st.pop(); if (!p && !q) continue; if ((p && !q) || (!p && q) || (p->val != q->val)) return false; st.push(p->right); st.push(q->right); st.push(p->left); st.push(q->left); } return true; } };
也可以使用中序遍歷的迭代寫法,對應(yīng)之前那道 Binary Tree Inorder Traversal,參見代碼如下:
解法三:
class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { stack<TreeNode*> st; while (p || q || !st.empty()) { while (p || q) { if ((p && !q) || (!p && q) || (p->val != q->val)) return false; st.push(p); st.push(q); p = p->left; q = q->left; } p = st.top(); st.pop(); q = st.top(); st.pop(); p = p->right; q = q->right; } return true; } };
對于后序遍歷的迭代寫法,貌似無法只是用一個棧來做,因為每次取出棧頂元素后不立馬移除,這樣使用一個棧的話兩棵樹結(jié)點的位置關(guān)系就會錯亂,分別使用各自的棧就好了,對應(yīng)之前那道 Binary Tree Postorder Traversal,參見代碼如下:
解法四:
class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { stack<TreeNode*> st1, st2; TreeNode *head1, *head2; while (p || q || !st1.empty() || !st2.empty()) { while (p || q) { if ((p && !q) || (!p && q) || (p->val != q->val)) return false; st1.push(p); st2.push(q); p = p->left; q = q->left; } p = st1.top(); q = st2.top(); if ((!p->right || p->right == head1) && (!q->right || q->right == head2)) { st1.pop(); st2.pop(); head1 = p; head2 = q; p = nullptr; q = nullptr; } else { p = p->right; q = q->right; } } return true; } };
對于層序遍歷的迭代寫法,其實跟先序遍歷的迭代寫法非常的類似,只不過把棧換成了隊列,對應(yīng)之前那道 Binary Tree Level Order Traversal,參見代碼如下:
解法五:
class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { queue<TreeNode*> que; que.push(p); que.push(q); while (!que.empty()) { p = que.front(); que.pop(); q = que.front(); que.pop(); if (!p && !q) continue; if ((p && !q) || (!p && q) || (p->val != q->val)) return false; que.push(p->right); que.push(q->right); que.push(p->left); que.push(q->left); } return true; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/100
類似題目:
Binary Tree Preorder Traversal
Binary Tree Postorder Traversal
Binary Tree Level Order Traversal
參考資料:
https://leetcode.com/problems/same-tree/
https://leetcode.com/problems/same-tree/discuss/32684/My-non-recursive-method
https://leetcode.com/problems/same-tree/discuss/32687/Five-line-Java-solution-with-recursion
到此這篇關(guān)于C++實現(xiàn)LeetCode(100.判斷相同樹)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)判斷相同樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用C++實現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點
利用C++實現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10數(shù)據(jù)結(jié)構(gòu)之AVL樹詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之AVL樹詳解,本文非常細(xì)致的講解了AVL樹的基礎(chǔ)知識、AVL樹的旋轉(zhuǎn)操作、AVL數(shù)的插入和刪除操作等,需要的朋友可以參考下2014-08-08C++實現(xiàn)LeetCode(119.楊輝三角之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(119.楊輝三角之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實現(xiàn)職工工資管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)職工工資管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03