1、题目描述
2、问题分析
判断一个节点,然后判断子树。
3、代码
bool isSubtree(TreeNode* s, TreeNode* t) {
if (s == NULL)
return false;
else {
return isSame(s,t) || isSubtree(s->left, t) || isSubtree(s->right, t);
} } bool isSame(TreeNode *t1, TreeNode *t2)
{
if (t1 == NULL && t2 == NULL) return true;
if (t1 == NULL || t2 == NULL) return false; return (t1->val == t2->val) && isSame(t1->left, t2->left) && isSame(t1->right , t2->right);
}