Skip to content

Instantly share code, notes, and snippets.

@GAierken
Last active October 28, 2020 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GAierken/a31d1e5596d336b73298c4dca5d4707e to your computer and use it in GitHub Desktop.
Save GAierken/a31d1e5596d336b73298c4dca5d4707e to your computer and use it in GitHub Desktop.
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