c++


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

Log in to reply
 

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