Skip to content

Instantly share code, notes, and snippets.

@dilanshah
Created February 27, 2018 19:44
Show Gist options
  • Save dilanshah/c1bbae1f767b8a91431d20ebdd522d70 to your computer and use it in GitHub Desktop.
Save dilanshah/c1bbae1f767b8a91431d20ebdd522d70 to your computer and use it in GitHub Desktop.
Leetcode 617. Merge Two Binary Trees. Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if not t1:
return t2
elif not t2:
return t1
t1.val += t2.val
t1.left = self.mergeTrees(t1.left, t2.left)
t1.right = self.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