Python Straightforward and Easy to understand


  • 0
    W

    There are probably more "pythonic" ways to do it but during interviews, thats the least of your worries. Somehow my solution beats 96% and is very straightforward, so I have decided to share

    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if not t1 and not t2:
            return None
        if not t1 and t2:
            return t2
        if t1 and 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

Log in to reply
 

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