/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void isSame(struct TreeNode* pRoot1, struct TreeNode* pRoot2, bool *sign)
{
if(pRoot2)
{
if(!pRoot1||pRoot2->val!=pRoot1->val)
{
*sign=false;
return;
}
isSame(pRoot1->left, pRoot2->left,sign);
isSame(pRoot1->right, pRoot2->right,sign);
}
}
bool res=false;
bool hasSubtree(struct TreeNode* pRoot1, struct TreeNode* pRoot2) {
if(pRoot1&&pRoot2)
{
if(pRoot1->val==pRoot2->val)
{
bool sign=true;
isSame(pRoot1,pRoot2,&sign);
if(sign)
{
res=true;
}
}
hasSubtree(pRoot1->left,pRoot2);
hasSubtree(pRoot1->right,pRoot2);
}
return res;
}
C语言 判断子树是否存在
最新推荐文章于 2024-08-31 18:18:12 发布