Javascript Solution


  • 0
    G

    var mergeTrees = function(t1, t2) {
    if(!t1 && !t2){return null;}
    var res;
    if(t1 && t2){
    res = new TreeNode(t1.val+t2.val);
    res.left = mergeTrees(t1.left,t2.left);
    res.right = mergeTrees(t1.right,t2.right);
    }
    else if(t1){
    res = new TreeNode(t1.val);
    res.left = mergeTrees(t1.left,null);
    res.right = mergeTrees(t1.right,null);
    }
    else if(t2){
    res = new TreeNode(t2.val);
    res.left = mergeTrees(null,t2.left);
    res.right = mergeTrees(null,t2.right);
    }
    return res;
    };


Log in to reply
 

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