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