题解 | #树的子结构#
树的子结构
http://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88
typedef struct TreeNode Node;
int IsEqual(Node* Root1, Node* Root2)
{
if (Root2 == NULL)
return 1;
if (Root1 == NULL)
return 0;
if (Root1->val != Root2->val)
return 0;
else
return(IsEqual(Root1->left, Root2->left) && IsEqual(Root1->right, Root2->right));
}
bool HasSubtree(struct TreeNode* pRoot1, struct TreeNode* pRoot2 ) {
int result = 0;
if (pRoot1 && pRoot2)
{
if (pRoot1->val == pRoot2->val)
result = IsEqual(pRoot1, pRoot2);
if (!result)
result = HasSubtree(pRoot1->left, pRoot2);
if(!result)
result = HasSubtree(pRoot1->right, pRoot2);
}
return result;
}