Beispiel #1
0
    def construct(length: int) -> Optional[Node]:
        nonlocal head

        if head is None or length == 0:
            return None

        left = construct(length // 2)
        root = Node(head.data)
        head = head.next
        root.left = left
        root.right = construct(length - length // 2 - 1)
        return root
Beispiel #2
0
    def tree() -> Optional[Node]:
        nonlocal index

        if index >= l:
            return None

        node = Node(preorder[index])
        index += 1

        if pre_ln[index - 1] == "N":
            node.left = tree()
            node.right = tree()

        return node
Beispiel #3
0
def merge_sum_tree(augend: Optional[Node],
                   addend: Optional[Node]) -> Optional[Node]:
    if augend is None and addend is None:
        return None

    if augend is None:
        return addend
    if addend is None:
        return augend

    root = Node(augend.data + addend.data)
    root.left = merge_sum_tree(augend.left, addend.left)
    root.right = merge_sum_tree(augend.right, addend.right)

    return root
    def tree(start: int, end: int) -> Optional[Node]:
        nonlocal pre_index

        if pre_index >= l or start > end:
            return None

        value = preorder[pre_index]
        node = Node(value)
        in_index = d.get(value) or -1
        pre_index += 1

        if start < end:
            node.left = tree(start, in_index - 1)
            node.right = tree(in_index + 1, end)

        return node
Beispiel #5
0
    def construct():
        nonlocal index, length

        if index >= length:
            return None

        root = Node(expression[index])
        index += 1

        if index < length - 1:
            if expression[index] == "?":
                index += 1
                root.left = construct()
                index += 1
                root.right = construct()

        return root
Beispiel #6
0
    def tree(start: int, end: int) -> Optional[Node]:
        nonlocal pre_index

        if start > end or pre_index >= l:
            return None

        node = Node(preorder[pre_index])
        pre_index += 1

        if start == end or pre_index >= l:
            return node

        mirror_index = d[preorder[pre_index]]

        if mirror_index <= end:
            node.left = tree(mirror_index, end)
            node.right = tree(start + 1, mirror_index - 1)

        return node
Beispiel #7
0
    def tree(start: int, end: int) -> Optional[Node]:
        nonlocal pre_index

        if pre_index >= l or start > end:
            return None

        value = preorder[pre_index]
        node = Node(value)
        pre_index += 1

        if start == end or pre_index >= l:
            return node

        post_index: int = d[preorder[pre_index]]

        if post_index <= end:
            node.left = tree(start, post_index)
            node.right = tree(post_index + 1, end)

        return node
            if in_iter == 0:
                node.data = arr[1]
            elif in_iter == l - 1:
                node.data = arr[l - 2]
            else:
                node.data = arr[in_iter - 1] + arr[in_iter + 1]
            in_iter += 1

        traverse_inorder(node.right, fill_array)

    arr: list = []
    traverse_inorder(root, fill_array=True)
    in_iter = 0
    l = len(arr)
    traverse_inorder(root, fill_array=False)
    return root


if __name__ == "__main__":
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.left = Node(6)
    root.right.right = Node(7)

    print(inorder(root))
    root = replace_data(root)
    print(inorder(root))
Beispiel #9
0

def diagonal_traversal(root: Node):
    queue = deque()
    queue.append([root, 0])

    while queue:
        node, level = queue.popleft()

        if node.left:
            queue.append([node.left, level + 1])
        if node.right:
            queue.appendleft([node.right, level])
        if queue and queue[0][1] > level:
            print(node.data)
        else:
            print(node.data, end=" ")


if __name__ == "__main__":
    root = Node(8)
    root.left = Node(3)
    root.right = Node(10)
    root.left.left = Node(1)
    root.left.right = Node(6)
    root.right.left = Node(7)
    root.right.right = Node(14)
    root.left.right.left = Node(4)
    root.right.right.left = Node(13)
    diagonal_traversal(root)
            second_queue.append(node_2.right)

        if node_1.right:
            first_queue.append(node_1.right)
            second_queue.append(node_2.left)

    if first_queue or second_queue:
        return False

    return True


if __name__ == "__main__":
    root = Node(1)
    root.left = Node(2)
    root.right = Node(2)
    root.left.left = Node(4)
    root.left.right = Node(3)
    root.right.left = Node(3)
    root.right.right = Node(4)
    assert is_mirror_recursive(root) is True
    assert is_mirror_iterative(root) is True

    root = Node(1)
    root.left = Node(2)
    root.right = Node(2)
    root.left.right = Node(3)
    root.right.right = Node(3)
    assert is_mirror_recursive(root) is False
    assert is_mirror_iterative(root) is False
Beispiel #11
0
from binary_tree_node import Node  # type: ignore


def sum_parent(root: Optional[Node], node: int) -> Tuple[int, bool]:
    if root is None:
        return 0, False

    left_sum, has_left_node = sum_parent(root.left, node)
    right_sum, has_right_node = sum_parent(root.right, node)
    result_sum = 0

    if has_left_node:
        result_sum += left_sum
    if has_right_node:
        result_sum += right_sum

    result_sum += int(root.data) if has_right_node or has_left_node else 0
    return result_sum, has_left_node or has_right_node or root.data == node


if __name__ == "__main__":
    root = Node(4)
    root.left = Node(2)
    root.right = Node(5)
    root.left.left = Node(7)
    root.left.right = Node(2)
    root.right.left = Node(2)
    root.right.right = Node(3)
    assert sum_parent(root, 2)[0] == 11
Beispiel #12
0
    path.append(root.data)
    k_sum_paths(root.left, k, path)
    k_sum_paths(root.right, k, path)

    total = 0

    for index in range(len(path) - 1, -1, -1):
        total += path[index]

        if total == k:
            print(path[index:])

    path.pop()


if __name__ == "__main__":
    root = Node(1)
    root.left = Node(3)
    root.left.left = Node(2)
    root.left.right = Node(1)
    root.left.right.left = Node(1)
    root.right = Node(-1)
    root.right.left = Node(4)
    root.right.left.left = Node(1)
    root.right.left.right = Node(2)
    root.right.right = Node(5)
    root.right.right.right = Node(2)

    k = 5
    k_sum_paths(root, k, [])
Beispiel #13
0
                   addend: Optional[Node]) -> Optional[Node]:
    if augend is None and addend is None:
        return None

    if augend is None:
        return addend
    if addend is None:
        return augend

    root = Node(augend.data + addend.data)
    root.left = merge_sum_tree(augend.left, addend.left)
    root.right = merge_sum_tree(augend.right, addend.right)

    return root


if __name__ == "__main__":
    augend = Node(2)
    augend.left = Node(1)
    augend.right = Node(4)
    augend.left.left = Node(5)

    addend = Node(3)
    addend.left = Node(6)
    addend.right = Node(1)
    addend.left.right = Node(2)
    addend.right.right = Node(7)

    root = merge_sum_tree(augend, addend)
    inorder(root)