def merge_two_binary_tress(t1, t2):
    node = None
    if t1 and t2:
        node = Node(t1.val + t2.val)
        node.left = merge_two_binary_tress(t1.left, t2.left)
        node.right = merge_two_binary_tress(t1.right, t2.right)
    elif t1:
        node = Node(t1.val)
        node.left = merge_two_binary_tress(t1.left, None)
        node.right = merge_two_binary_tress(t1.right, None)
    elif t2:
        node = Node(t2.val)
        node.left = merge_two_binary_tress(t2.left, None)
        node.right = merge_two_binary_tress(t2.right, None)
    return node
Exemple #2
0
def constructMaximumBinaryTree(nums):
    size = len(nums)
    if (size != 0):
        max_index = max(nums)
        root = Node(nums[max_index])
        if (max_index > 0):
            leftarr = nums[0:max_index]
            root.left = constructMaximumBinaryTree(leftarr)
        else:
            root.left = None
        if (max_index < size - 1):
            rightarr = nums[max_index + 1:size]
            root.right = constructMaximumBinaryTree(rightarr)
        else:
            root.right = None
        return root
    return None