C++實現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
判斷兩棵樹是否相同和之前的判斷兩棵樹是否對稱都是一樣的原理,利用深度優先搜索 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); } };
這道題還有非遞歸的解法,因為二叉樹的四種遍歷(層序,先序,中序,後序)均有各自的迭代和遞歸的寫法,這裡我們先來看先序的迭代寫法,相當於同時遍歷兩個數,然後每個節點都進行比較,可參見之間那道 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; } };
也可以使用中序遍歷的迭代寫法,對應之前那道 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; } };
對於後序遍歷的迭代寫法,貌似無法隻是用一個棧來做,因為每次取出棧頂元素後不立馬移除,這樣使用一個棧的話兩棵樹結點的位置關系就會錯亂,分別使用各自的棧就好瞭,對應之前那道 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; } };
對於層序遍歷的迭代寫法,其實跟先序遍歷的迭代寫法非常的類似,隻不過把棧換成瞭隊列,對應之前那道 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 Inorder 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
到此這篇關於C++實現LeetCode(100.判斷相同樹)的文章就介紹到這瞭,更多相關C++實現判斷相同樹內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- C++實現LeetCode(107.二叉樹層序遍歷之二)
- C++實現LeetCode(102.二叉樹層序遍歷)
- C++實現LeetCode(144.二叉樹的先序遍歷)
- C++實現LeetCode(156.二叉樹的上下顛倒)
- C++實現LeetCode(103.二叉樹的之字形層序遍歷)