4 lines C++ DFS


  • 0
    class Solution {
    public:
        bool isSubtree(TreeNode* s, TreeNode* t) {
            if(!s) return !t;
            return isEqual(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
        }
        
        bool isEqual(TreeNode* p, TreeNode* t){
            if(!p || !t) return !p && !t;
            return p->val == t->val && isEqual(p->left, t->left) && isEqual(p->right, t->right);
        }
    };
    

Log in to reply
 

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