Python recursion solution

  • 0
    class Solution(object):
        def mergeTrees(self, t1, t2):
            if (t1 is None) and (t2 is None):
                return None
            t = TreeNode((t1.val if t1 else 0) + (t2.val if t2 else 0))
            t.left = self.mergeTrees(t1.left if t1 else None, t2.left if t2 else None)
            t.right = self.mergeTrees(t1.right if t1 else None, t2.right if t2 else None)
            return t

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.