查找二叉树A中是否有子树B

//查找二叉树A中是否有子树B
bool HashSubtree(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2)
{
	bool result=false;
	if(pRoot1!=NULL&&pRoot2!=NULL)
	{
		if(pRoot1->m_nValue==pRoot2->m_nValue)
			result=DoesTreeHaveTree2(pRoot1,pRoot2);
		if(!result)
			result=HashSubtree(pRoot1->m_pLeft,pRoot2);
		if(!result)
			result=HashSubtree(pRoot1->m_pReft,pRoot2);
	}
	return result;
}

bool DoesTreeHaveTree2(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2)
{
	if(pRoot2==NULL)
	  return true;
	if(pRoot1==NULL)
	  return false;
	if(pRoot1->m_nValue!=pRoot2->m_nValue)
	  return false;
	return DoesTreeHaveTree2(pRoot1->m_pLeft,pRoot2->m_pLeft)&&DoesTreeHaveTree2(pRoot1->m_pReft,pRoot2->m_RLeft)
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/sun19910114/article/details/38928993
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞