Пример #1
0
def minimal_tree(lst):
    """time: O(n) space: O(n)"""
    if not lst: return None
    middle = len(lst) // 2
    root = BinaryNode(lst[middle])
    root.left = minimal_tree(lst[:middle])
    root.right = minimal_tree(lst[middle+1:])
    return root
Пример #2
0
def test_complex(fn):

    #      3
    #     / \
    #    2   5
    #   /   / \
    #  1   4   6

    n1 = BinaryNode(1)
    n2 = BinaryNode(2, left=n1)
    n4 = BinaryNode(4)
    n6 = BinaryNode(6)
    n5 = BinaryNode(5, left=n4, right=n6)
    root = BinaryNode(3, left=n2, right=n5)
    assert fn(root) == True

    #      3
    #     / \
    #    2   5
    #   /   / \
    #  1   4   6
    #           \
    #            7

    n7 = BinaryNode(7)
    n6.right = n7
    assert fn(root) == True

    #      3
    #     / \
    #    2   5
    #   /   / \
    #  1   4   6
    #           \
    #            7
    #             \
    #              8

    n8 = BinaryNode(8)
    n7.right = n8
    assert fn(root) == False