码迷,mamicode.com
首页 > 其他好文 > 详细

树的子结构*

时间:2021-02-18 13:43:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:树的子结构   bre   ret   turn   val   cpp   node   sub   binary   

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);
}

  

树的子结构*

标签:树的子结构   bre   ret   turn   val   cpp   node   sub   binary   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!