# A few lines of JavaScript

• ``````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;
};
``````

• @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.

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

• @ChienWeiLuo any time, bro :).

• @loctn so cool

• Cool solution. Saved a lot of conditional statements.

• 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;
};
``````

• @yfang111 nice.

• @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.

• @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.

• @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.

• @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.

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

• @giltwg any time :).

• Thanks, that helped

• Hi!

Nevermind, I forgot the null when returning, it was expected for the printer
I came up with a solution that's basically the same as you two:

``````
var mergeTrees = function(t1, t2) {
if (!t1 && !t2) return null
if (!t1 || !t2) return 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
};
``````

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