Last active
October 28, 2020 02:39
-
-
Save GAierken/a31d1e5596d336b73298c4dca5d4707e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mergeTrees = (t1, t2) => { | |
//when there is null given tree | |
if(!t1 || !t2) return t1 || t2 | |
//both of trees are not null, | |
//we sum the value, and transform t1, | |
//we can also choose t2, and return t2 at the end | |
t1.val += t2.val | |
//recursively traverse left path | |
t1.left = mergeTrees(t1.left, t2.left) | |
//recursively traverse right path | |
t1.right = mergeTrees(t1.right, t2.right) | |
//return merged tree | |
return t1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment