剑指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);
    }
};
posted @ 2018-08-25 21:20  一条图图犬  阅读(77)  评论(0编辑  收藏  举报