5-line Python

  • 0
    class Solution(object):
        def mergeTrees(self, t1, t2):
            :type t1: TreeNode
            :type t2: TreeNode
            :rtype: TreeNode
            if t1 or t2:
                root = TreeNode((t1 and t1.val or 0) + (t2 and t2.val or 0))
                root.left = self.mergeTrees(t1 and t1.left, t2 and t2.left)
                root.right = self.mergeTrees(t1 and t1.right, t2 and t2.right)
                return root

Log in to reply

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