#include "iostream"
using namespace std;
struct BinaryTreeNode{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};
bool doesTree1HasTree2(BinaryTreeNode *pRoot1, BinaryTreeNode *pRoot2)
{
if (pRoot2 == NULL)
return true;
if (pRoot1 == NULL)
return false;
if (pRoot1->data != pRoot2->data)
return false;
return doesTree1HasTree2(pRoot1->left, pRoot2->left)&&doesTree1HasTree2(pRoot1->right, pRoot2->right) ;
}
bool hasSubTree(BinaryTreeNode *pRoot1,BinaryTreeNode *pRoot2)
{
bool result = false;
if (pRoot1!=NULL&&pRoot2!=NULL)
{
if (pRoot1->data == pRoot2->data)
result = doesTree1HasTree2(pRoot1, pRoot2);
if (!result)
result=hasSubTree(pRoot1->left, pRoot2);
if (!result)
result = hasSubTree(pRoot1->right, pRoot2);
}
return result;
}
int main()
{
system("pause");
return 0;
}
剑指offer面试题18:树的子结构
最新推荐文章于 2023-12-13 00:15:00 发布