Beispiel #1
0
def test_nontrivial():
    #       3
    #      / \
    #     5   1
    #    / \   \
    #   6   2   8
    #      / \
    #     7   4
    root = TreeNode(3)
    target = TreeNode(5)
    root.left = target
    target.left = TreeNode(6)
    target.right = TreeNode(2, TreeNode(7), TreeNode(4))
    root.right = TreeNode(1, None, TreeNode(8))
    res = all_nodes_k_distance(root, target, 2)
    assert set([1, 4, 7]) == set(res)
Beispiel #2
0
def test_nontrivial_true():
    #        4               4
    #      /   \           /   \
    #     2     7   ==    2     7
    #    / \   / \       / \   / \
    #   1   3 6   9     1   3 6   9

    tree1 = TreeNode(4)
    tree1.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    tree1.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    tree2 = TreeNode(4)
    tree2.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    tree2.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    assert is_same_tree(tree1, tree2)
Beispiel #3
0
def test_nontrivial_false():
    #        4               4
    #      /   \           /   \
    #     2     7   !=    7     2
    #    /     / \       / \   / \
    #   1     6   9     9   6 3   1

    tree1 = TreeNode(4)
    tree1.left = TreeNode(2, left=TreeNode(1))
    tree1.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    tree2 = TreeNode(4)
    tree2.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    tree2.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    assert not is_same_tree(tree1, tree2)
Beispiel #4
0
def test_nontrivial_true():
    #                            4
    #                          /   \
    #     2   is subtree of   2     7
    #    / \                 / \   / \
    #   1   3               1   3 6   9

    tree1 = TreeNode(2)
    tree1.left = TreeNode(1)
    tree1.right = TreeNode(3)

    tree2 = TreeNode(4)
    tree2.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    tree2.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    assert is_subtree(tree1, tree2)
Beispiel #5
0
def test_nontrivial():
    #        4               4
    #      /   \           /   \
    #     2     7   ->    7     2
    #    / \   / \       / \   / \
    #   1   3 6   9     9   6 3   1

    tree = TreeNode(4)
    tree.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    tree.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    tree_inv = TreeNode(4)
    tree_inv.left = TreeNode(7, left=TreeNode(9), right=TreeNode(6))
    tree_inv.right = TreeNode(2, left=TreeNode(3), right=TreeNode(1))

    assert is_same_tree(invert(tree), tree_inv)
Beispiel #6
0
def _array_to_bst(L, first, last):
    if first > last:
        return None
    mid = (first + last) // 2
    node = TreeNode(L[mid])
    node.left = _array_to_bst(L, first, mid - 1)
    node.right = _array_to_bst(L, mid + 1, last)
    return node
Beispiel #7
0
def test_preorder():
    #       4
    #      / \
    #     2   5
    #    / \
    #   1   3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    root.right = TreeNode(5)
    assert preorder(root) == [4, 2, 1, 3, 5]
Beispiel #8
0
def test_nontrivial_true():
    #       4
    #      / \
    #     2   5
    #    / \
    #   1   3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    root.right = TreeNode(5)
    assert is_bst(root)
def test_nontrivial():
    #       4
    #      / \
    #     2   5
    #    / \
    #   1   3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    root.right = TreeNode(5)
    assert get_path(root, 3) == [4, 2, 3]
Beispiel #10
0
def test_inorder():
    #       4
    #      / \
    #     2   5
    #    / \
    #   1   3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    root.right = TreeNode(5)
    assert inorder(root) == [1, 2, 3, 4, 5]
Beispiel #11
0
def test_postorder():
    #       4
    #      / \
    #     2   5
    #    / \
    #   1   3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    root.right = TreeNode(5)
    assert postorder(root) == [1, 3, 2, 5, 4]
Beispiel #12
0
def test_nontrivial_false():
    #       3
    #      / \
    #     2   5
    #    / \
    #   1   4
    root = TreeNode(3)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(4))
    root.right = TreeNode(5)
    assert not is_bst(root)
Beispiel #13
0
def test_nontrivial():
    #       4
    #      / \
    #     2   5
    #    / \
    #   1   3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    root.right = TreeNode(5)
    assert get_height(root) == 3
def test_nontrivial():
    #       4
    #      / \
    #     2   5
    #    /     \
    #   1       3
    root = TreeNode(4)
    root.left = TreeNode(2, left=TreeNode(1))
    root.right = TreeNode(5, right=TreeNode(3))
    traversal = level_order_traversal(root)
    assert traversal == [4, 2, 5, 1, 3]
Beispiel #15
0
def test_nontrivial():
    #        5
    #      /   \
    #     3     8
    #    / \
    #   2   4

    tree = TreeNode(4)
    tree.left = TreeNode(3, left=TreeNode(2), right=TreeNode(4))
    tree.right = TreeNode(8)

    assert find_min(tree) == 2
Beispiel #16
0
def test_nontrivial_false():
    #        5
    #      /   \
    #     3     8
    #    / \
    #   2   4

    tree = TreeNode(4)
    tree.left = TreeNode(3, left=TreeNode(2), right=TreeNode(4))
    tree.right = TreeNode(8)

    assert not search(tree, 1)
Beispiel #17
0
def test_nontrivial_true():
    #        6
    #      /   \
    #     2     8
    #    / \   / \
    #   1   3 7   9
    L = [1, 2, 3, 6, 7, 8, 9]
    tree = array_to_bst(L)
    expected = TreeNode(6)
    expected.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    expected.right = TreeNode(8, left=TreeNode(7), right=TreeNode(9))
    assert is_same_tree(tree, expected)
Beispiel #18
0
def test_nontrivial():
    #        4
    #      /   \
    #     2     7
    #    / \   / \
    #   1   3 6   9

    tree = TreeNode(4)
    tree.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3))
    tree.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9))

    lca = get_lca(tree, 1, 3)
    assert lca.val == 2
Beispiel #19
0
def test_nontrivial():
    #        5
    #      /   \
    #     3     8
    #    / \
    #   2   4

    tree = TreeNode(5)
    tree.left = TreeNode(3, left=TreeNode(2), right=TreeNode(4))
    tree.right = TreeNode(8)

    lca = get_lca(tree, 2, 4)
    assert lca.val == 3