572. Subtree of Another Tree

先盘对两个数是不是一样的树,不是的话去看是不是和s->left,s->right一样的树。

bool isSametree(struct TreeNode* s, struct TreeNode* t) {

    if(s == NULL && t == NULL) return true;
    if(s == NULL || t == NULL)  return false;
    if(s->val != t->val) return false;

    return isSametree(s->left,t->left) && isSametree(s->right, t->right);
}


bool isSubtree(struct TreeNode* s, struct TreeNode* t) {
    if(s == NULL)
        return false;
 
    if(isSametree(s,t))
        return true;

    return isSubtree(s->left,t) || isSubtree(s->right,t);

}
···

你可能感兴趣的:(572. Subtree of Another Tree)