Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Practice] Merge two binary trees #38

Open
lpatmo opened this issue Apr 9, 2019 · 1 comment
Open

[Practice] Merge two binary trees #38

lpatmo opened this issue Apr 9, 2019 · 1 comment

Comments

@lpatmo
Copy link
Member

lpatmo commented Apr 9, 2019

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

@lpatmo
Copy link
Member Author

lpatmo commented Apr 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant