My Python Solution(158ms)


  • 0
    A
    class Solution(object):
        def mergeTrees(self, t1, t2):
            """
            :type t1: TreeNode
            :type t2: TreeNode
            :rtype: TreeNode
            """
            def travel(root1,root2):
                if root1!=None and root2!=None:
                    root = TreeNode(root1.val+root2.val)
                    root.left = travel(root1.left,root2.left)
                    root.right = travel(root1.right,root2.right)
                elif root1==None and root2!=None:
                    root = TreeNode(root2.val)
                    root.left = root2.left
                    root.right = root2.right
                elif root2==None and root1!=None:
                    root = TreeNode(root1.val)
                    root.left = root1.left
                    root.right = root1.right
                else:
                    root = None
                return root
            return travel(t1,t2)
    

Log in to reply
 

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