naive solution


  • 0
    C
    def mergeTrees(self, t1, t2):
            """
            :type t1: TreeNode
            :type t2: TreeNode
            :rtype: TreeNode
            """
            
            if t1 is None and t2 is None:
                return
            
            # t1 is None
            if t1 is None:
                return t2
            
            # t2 is None
            if t2 is None:
                return t1
            
            # t1 and t2 are not None
            new_node = TreeNode(t1.val + t2.val)
            new_node.left = self.mergeTrees(t1.left, t2.left)
            new_node.right = self.mergeTrees(t1.right, t2.right)
            
            return new_node
    

Log in to reply
 

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