WEI-CHENG CHEN

解題

給兩個二元數,判斷是否相同

解法

給兩棵樹判斷是否相同,會有以下情況:

class Solution {
public:
    bool DFS(TreeNode* p, TreeNode* q){
        if(!p && !q) return true;
        else if(!p || !q) return false;
        else if(p->val != q->val) return false;
        else{
            return DFS(p->left, q->left) && DFS(p->right, q->right);
        }
    }
    bool isSameTree(TreeNode* p, TreeNode* q) {
        return DFS(p, q);
    }
};