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

来源:互联网 发布:淘宝站内免费推广资源 编辑:程序博客网 时间:2024/05/21 00:56
//查找二叉树A中是否有子树Bbool 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)}

0 0
原创粉丝点击