Simple Python solution (Recursive)


  • 0
    P
    class Solution(object):
        def mergeTrees(self, t1, t2):
            
            if not t1 and not t2:
                return None
            
            elif t1 and not t2:
                return t1
            
            elif t2 and not t1:
                return t2
            
            r = TreeNode(t1.val + t2.val)
            r.left = self.mergeTrees(t1.left, t2.left)
            r.right = self.mergeTrees(t1.right, t2.right)
            return r
    
    # 183 / 183 test cases passed.
    # 136 ms
    # Percentile (N/A): Leetcode isn't showing percentile graph probably because this problem was just added via a contest.
    

Log in to reply
 

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