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

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);
}

  

posted @ 2013-01-09 19:55  代码改变未来  阅读(572)  评论(0编辑  收藏  举报