Anybody know what is wrong with this code? I am new to C++ and don't know why this code returns nullptr for me. Thanks in advance!


  • 0
    S

    '''
    class Solution {
    public:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
    if(t1==nullptr && t2==nullptr)
    {
    return nullptr;
    }
    else if(t1!=nullptr && t2!=nullptr)
    {
    TreeNode node((t1->val)+(t2->val));
    node.left=mergeTrees(t1->left, t2->left);
    node.right=mergeTrees(t1->right, t2->right);
    return &node;
    }
    else
    {
    return t1==nullptr? t2:t1;
    }

    }
    

    };
    '''


Log in to reply
 

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