题目链接 572. 另一棵树的子树
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {if (p != NULL && q != NULL) {return p->val == q->val // 分解比较根左右&& isSameTree(p->left, q->left)&& isSameTree(p->right, q->right);}else if (p == NULL && q == NULL) {return true;}else { // 一个为NULL,另一个不为NULLreturn false;}
}bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){if (root != NULL) { // 子树是否等于树本身,或等于树的左子树或右子树return isSameTree(root, subRoot)|| isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);}return false;
}