Very simple python recursive tranverse


  • 0
    class Solution(object):
        def mergeTrees(self, t1, t2):
            """
            :type t1: TreeNode
            :type t2: TreeNode
            :rtype: TreeNode
            """
            if t1 is None:
                return t2
            elif t2 is None:
                return t1
            self.help(t1, t2)
            return t1
            
        def help(self, t1, t2):
            t1.val += t2.val
            if t2.left is not None:
                if t1.left is None:
                    t1.left = TreeNode(0)
                self.help(t1.left, t2.left)
            if t2.right is not None:
                if t1.right is None:
                    t1.right = TreeNode(0)
                self.help(t1.right, t2.right)

Log in to reply
 

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