LeetCode | 572. 另一棵树的子树
OJ链接
- 我们需要判断两棵二叉树是否相同,如果再判断的的时候不同我们就直接返回false,否则就返回true
- 然后再检查左子树和右子树里面是否存在subRoot子树~~
bool isSameTree(struct TreeNode* q, struct TreeNode* p)
{if(q == NULL && p == NULL)return true;if(q == NULL || p == NULL)return false;if(q->val != p->val)return false;return isSameTree(q->left,p->left)&& isSameTree(q->right,p->right);
}bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){if(root == NULL)return NULL;if(isSameTree(root,subRoot))return true;return isSubtree(root->left,subRoot)|| isSubtree(root->right,subRoot);
}