C simple solution with recursion


  • 0
    V
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    bool isSameTree(struct TreeNode * p, struct TreeNode * q) 
    {
        if (p == NULL)
        {
            if (q == NULL)
                return true;
                
            return false;
        }
        else if (q == NULL)
            return false;
            
        if (p->val != q->val)
            return false;
        
        bool same_left, same_right;
        
        same_left = isSameTree(p->left, q->left);
        same_right = isSameTree(p->right, q->right);
        
        return same_left & same_right;
    }
    

Log in to reply
 

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