C++ Recursive Solution 19ms-98%


  • 0
    I
        bool isSubtree(TreeNode* s, TreeNode* t) {
            if(s == NULL && s == t)
                return true;
            
            if((!s && t) || (s && !t))
                return false;
            //cout<<s->val<<" "<<t->val<<endl;
            if(s->val == t->val && (s->left != NULL ? s->left->val:0) == (t->left != NULL ? t->left->val:0))
                 if(isSubtree(s->left,t->left)&&isSubtree(s->right,t->right))
                     return true;
            
            return isSubtree(s->left,t)||isSubtree(s->right,t);
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.