class Solution {
public:
bool check(TreeNode* r1, TreeNode* r2)
{
if(r2==NULL) return true;//如果r2为空,无论r1,都匹配成功
if(r1&&r2)
{
if(r1->val!=r2->val) return false;
else return check(r1->left,r2->left)&&check(r1->right,r2->right);
}
else return false;//如果r1为空,r2还存在,返回false
}
bool dfs(TreeNode* r1, TreeNode* r2)
{
if(r1==NULL||r2==NULL) return false;//空树不是任何子结构
if(check(r1,r2))
return true;
return dfs(r1->left,r2)||dfs(r1->right,r2);
}
bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
return dfs(pRoot1,pRoot2);
}
};
标签:TreeNode,r1,r2,子结构,return,dfs,false
From: https://www.cnblogs.com/tangxibomb/p/17263206.html