首页 > 其他 > 详细

树的子结构*

时间:2021-02-16 23:23:09      阅读:25      评论:0      收藏:0      [点我收藏+]
struct BinaryTreeNode
{
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

bool HasSubTree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
{
	bool bret = false;
	if (pRoot1 != nullptr && pRoot2 != nullptr)
	{
		if (pRoot1->m_nValue == pRoot2->m_nValue)
			bret = DoesTree1HasTree2(pRoot1, pRoot2);
		if (!bret)
			bret = HasSubTree(pRoot1->m_pLeft, pRoot2);
		if (!bret)
			bret = HasSubTree(pRoot1->m_pRight, pRoot2);
	}
	return bret;
}

bool DoesTree1HasTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
{
	if (pRoot2 == nullptr)
		return true;
	if (pRoot1 == nullptr)
		return false;

	if (pRoot1->m_nValue != pRoot2->m_nValue)
		return false;

	return DoesTree1HasTree2(pRoot1->m_pLeft, pRoot2->m_pLeft) && DoesTree1HasTree2(pRoot1->m_pRight, pRoot2->m_pRight);
}

  

树的子结构*

原文:https://www.cnblogs.com/yapp/p/14407491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!