A few lines of JavaScript


  • 4
    var mergeTrees = function(t1, t2) {
        if (!t1 && !t2) return null;
        const root = new TreeNode(((t1 || 0).val || 0) + ((t2 || 0).val || 0));
        root.left = mergeTrees(t1 && t1.left, t2 && t2.left);
        root.right = mergeTrees(t1 && t1.right, t2 && t2.right);
        return root;
    };
    

  • 0
    C

    @loctn Is this right? Can you give a comment about this? :)


  • 1

    @ChienWeiLuo I'd be happy to explain.

    Since we return null when both t1 and t2 are null (do not exist), we have to account for the fact that one of them may be null.

    (t1 || 0).val gets the value of the t1 node when t1 exists, otherwise it gets undefined since (0).val is undefined. Then because that is falsy, ((t1 || 0).val || 0) is 0 when t1 doesn't exist. We are using the fact that falsyValue || someValue evaluates to someValue, and this allows us to create that short line that would otherwise be a longer ternary or multi-line conditional.

    We use a similar technique for t1 && t1.left since falsyValue && someValue evaluates to falsyValue, and therefore null gets passed to the next recursion if t1 doesn't exist.

    The algorithm itself is a standard preorder DFS. Hope this helps.


  • 0
    C

    @loctn Come to Understand.Thank for the detailed interpretation, bro!


  • 0

    @ChienWeiLuo any time, bro :).


  • 0
    L

    @loctn so cool


  • 0
    E

    Cool solution. Saved a lot of conditional statements.


  • 0
    Y

    Could've been a little cleaner

    var mergeTrees = function(t1, t2) {
        if (t1 && t2) {
            const newNode = new TreeNode(t1.val + t2.val);
            newNode.left = mergeTrees(t1.left, t2.left);
            newNode.right = mergeTrees(t1.right, t2.right);
            return newNode;
        } 
        return t1 || t2;
    };
    

  • 0

    @yfang111 nice.


  • 0
    G

    @loctn
    What's the different between (t1 || 0) and ((t1 || 0).val || 0)?

    null || 0 =>0
    undefined || 0 => 0

    I run these two in console and they have the same result.

    Thanks.


  • 1

    @giltwg we need to add the values when they exist. When they don't exist they are null which doesn't have properties since null is not an object but its own type (not obvious because typeof null gives 'object'), so we have to "or" it to 0 because (0).val is undefined which is falsy and will contribute zero to the sum.


  • 0
    G

    @loctn My point is : Null is also a falsy value. So (null || 0) will also contribute zero to the sum. Why should we use undefined? Thanks.


  • 1

    @giltwg because the times when the node is not falsy, there is a value there accessed by .val. We use undefined simply because that is the value of a nonexistent property, in this case the val property of 0, but we don't prefer it for any other particular reason. Basically, it doesn't matter what the second term is in (t1 || 0) - we could have used (t1 || {}) - but I chose 0 for brevity.


  • 0
    G

    @loctn now it is clear, thanks for your explanation.


  • 0

    @giltwg any time :).


Log in to reply
 

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