题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
bool hehe(TreeNode* root1,TreeNode* root2)
{
if(root2 == NULL) return true;
if(root1->val != root2->val) return false;
if(root1 == NULL) return false;
bool flag1,flag2;
flag1 = flag2 = false;
flag1 = hehe(root1->left,root2->left);
flag2 = hehe(root1->right,root2->right);
return flag1 && flag2;
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if(pRoot2 == NULL || pRoot1 == NULL) return false;
bool flag1,flag2,flag3;
flag1 = flag2 = false;
flag1 = HasSubtree(pRoot1->left,pRoot2);
flag2 = HasSubtree(pRoot1->right,pRoot2);
if(pRoot1->val == pRoot2->val)
{
flag3 = hehe(pRoot1,pRoot2);
}
return flag1 || flag2 || flag3;
}
来源:https://blog.csdn.net/qq_40800823/article/details/99167312