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);
}
};
這道題還有非遞歸的解法,因為二叉樹的四種遍歷(層序,先序,中序,后序)均有各自的迭代和遞歸的寫法,這里我們先來看先序的迭代寫法,相當于同時遍歷兩個數,然后每個節(jié)點都進行比較,可參見之間那道 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 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++實現(xiàn)LeetCode(100.判斷相同樹)的文章就介紹到這了,更多相關C++實現(xiàn)判斷相同樹內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++實現(xiàn)LeetCode(119.楊輝三角之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(119.楊輝三角之二),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07

