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

  

原文地址:https://www.cnblogs.com/tgkx1054/p/2853589.html