fast python solution

  • 0

    I noticed this beat 98% of code, so I thought I'd post it.

    edit: seems like the performance really varies depending on the test case. I'll leave this here anyway in case it helps someone

        def mergeTrees(self, t1, t2):
            :type t1: TreeNode
            :type t2: TreeNode
            :rtype: TreeNode
            if(not t1):
                return t2
            if(not t2): 
                return t1
            if(t1 and t2):
                t1.val += t2.val
                t1.left = self.mergeTrees(t2.left, t1.left)
                t1.right = self.mergeTrees(t2.right, t1.right)
                return t1

Log in to reply

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