剑指offer 数的子结构

class Solution {
public:
    bool isSame(TreeNode* a, TreeNode* b) {
        if (a != nullptr && b != nullptr) {
            if (a->val == b->val) {
                return isSame(a->left, b->left) && isSame(a->right, b->right);
            } else {
                return false;
            }
        }
        else return !(a == nullptr && b != nullptr);
    }
    
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(pRoot1 == nullptr || pRoot2 == nullptr){
            return false;
        }
        if(pRoot1->val == pRoot2->val && isSame(pRoot1, pRoot2)){
            return true;
        }
        return HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
    }
};
原文地址:https://www.cnblogs.com/theodoric008/p/9535415.html