您的位置:首页 > 职场人生

剑指offer面试题18:二叉树的子结构

2013-01-09 19:55 387 查看
struct BinaryTreeNode
{
int value;
BinaryTreeNode *left,*right;
};
bool doestree1havetree2(BinaryTreeNode *root1,BinaryTreeNode *root2);
bool hassubtree(BinaryTreeNode *root1,BinaryTreeNode *root2)
{
bool result=false;
if(root1!=NULL&&root2!=NULL)
{
if(root1->value==root2->value)
{
result=doestree1havetree2(root1,root2);
}
if(!result)result=hassubtree(root1->left,root2);
if(!result)result=hassubtree(root1->right,root2);
}
return result;
}
bool doestree1havetree2(BinaryTreeNode *root1,BinaryTreeNode *root2)
{
if(root2==NULL)
return true;
if(root1==NULL)
return false;
if(root1->value!=root2->value)
return false;
return doestree1havetree2(root1->left,root2->left)&&doestree1havetree2(root1->right,root2->right);
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: