class Solution {
public:
bool check(TreeNode* pRoot1, TreeNode* pRoot2){
if(pRoot2 == nullptr){
return true;
}
if(pRoot1 == nullptr){
return false;
}
if(pRoot1->val != pRoot2->val){
return false;
}else {
return check(pRoot1->left, pRoot2->left) && check(pRoot1->right, pRoot2->right);
}
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
//因为接下来要访问pRoot1->val和pRoot2->val, 所以提前处理nullptr的情况
if(pRoot1 == nullptr || pRoot2 == nullptr) return false;
if(pRoot1->val == pRoot2->val && check(pRoot1, pRoot2)){
return true;
}
return HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
}
};