codebuddies / DailyAlgorithms

Do a problem. Create (or find) your problem in the issues. Paste a link to your solution. See others' solutions of the same problem.
12 stars 1 forks source link

[Practice] Merge two binary trees #38

Open lpatmo opened 5 years ago

lpatmo commented 5 years ago

https://leetcode.com/problems/merge-two-binary-trees/submissions/

lpatmo commented 5 years ago

var mergeTrees = function(t1, t2) {
    //if both trees have no children, return sum of root nodes
    if (t1 && !t2) { return t1; }
    if (!t1 && t2) { return t2; }
    if (!t1 && !t2) { return null; }

    t1.val += t2.val;
    t1.left = mergeTrees(t1.left, t2.left);
    t1.right = mergeTrees(t1.right, t2.right);
    return t1;

};