What is wrong with this piece of code in JS?


  • 0
    S

    What is wrong with this code? Leetcode kept throw "runtime error" when submit this piece of code...

    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} t1
     * @param {TreeNode} t2
     * @return {TreeNode}
     */
    var mergeTrees = function(t1, t2) {
        if(!t1 && !t2){
            return undefined;
        }
        if(!t1){
            return t2;
        }
        if(!t2){
            return t1;
        }
        
        var merged = new TreeNode(t1.val + t2.val);
        merged.left = mergeTrees(t1.left, t2.left);
        merged.right = mergeTrees(t1.right, t2.right);
        
        return merged;
    };
    

Log in to reply
 

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