树的子结构




bool HasSubtree(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 = HasSubtree(pRoot1->m_pLeft,pRoot2);
if (!result)
result = HasSubtree(pRoot1->m_pRight, pRoot2);
}
return result;
}



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




阅读更多
个人分类: 剑指offer
上一篇17.合并两个排序的链表
下一篇19.二叉树的镜像
博主设置当前文章不允许评论。

没有更多推荐了,返回首页

关闭
关闭