DFS simple solution


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

Log in to reply
 

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