[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

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM