树的子结构*

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

  

posted on 2021-02-16 20:53  Noora&w  阅读(22)  评论(0编辑  收藏  举报