Recursive JavaScript Solution


  • 0
    Z
    var mergeTrees = function(t1, t2) {
        
        if(t1 === null && t2 === null)
            return null;
        
        if(t1 === null)
            t1 = new TreeNode(0);
    
        if(t2 === null)
            t2 = new TreeNode(0);
        
        let node = new TreeNode(t1.val + t2.val);
        node.left = mergeTrees(t1.left, t2.left);
        node.right = mergeTrees(t1.right, t2.right);
        
        return node;
    };
    

Log in to reply
 

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