示例#1
0
def main():
    root = BinaryTreeNode(1)
    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)
    left_view = []
    get_left_view(root, 0, left_view)
    print(left_view)
示例#2
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    vertical_order = get_vertical_order(root)
    vertical_order = [node.data for node in vertical_order]
    print(vertical_order)
示例#3
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    right_view = []
    get_right_view(root, 0, right_view)
    print(right_view)
示例#4
0
def main():
    root = BinaryTreeNode(1)

    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)
    print(Tree(root))
    print(get_max_path(root))
示例#5
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    xpos_nodes_map = {}
    top_view = get_top_view(root)
    print(top_view)
示例#6
0
def main():
    root = BinaryTreeNode(1)
    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)
    bottom_view = {}
    get_bottom_view(root, 0, 0, bottom_view)
    print(bottom_view)
示例#7
0
def main():
    root = BinaryTreeNode(1)
    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)
    root2 = BinaryTreeNode(100)
    root2.left = root
    print(Tree(root2))
    print(get_max_path_leaves(root2))
示例#8
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.right.right = Node(5)
    order = postorder_iterative(root)
    print(order)
示例#9
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.right.right = Node(5)
    order = []
    preorder(root, order)
    print(order)
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.right.right = Node(5)
    level_spiral_traversal = get_level_order_spiral_traversal(root)
    level_spiral_traversal = [node.data for node in level_spiral_traversal]
    print(level_spiral_traversal)
示例#11
0
 def reverse_inorder(root: Node):
     nonlocal prev
     if root is None:
         return
     reverse_inorder(root.left)
     root.right = prev
     if prev is not None:
         prev.left = root
     prev = root
     reverse_inorder(root.right)
示例#12
0
def main():
    root = BinaryTreeNode(20)
    root.left = BinaryTreeNode(10)
    root.left.left = BinaryTreeNode(5)
    root.left.left.left = BinaryTreeNode(1)
    root.left.left.left.right = BinaryTreeNode(50)
    root = BinaryTreeNode(1)
    root.left = BinaryTreeNode(3)
    root.right = BinaryTreeNode(2)
    print(Tree(root))
    print(isBST_direct(root))
示例#13
0
def deserialize_tree(arr, idx=0):
    '''
    idx is the index of the current element to be processed.
    Returns <root, idx> where idx is the index of the next element to be processed.
    '''
    if idx >= len(arr) or arr[idx] is None:
        return None, idx + 1
    node = Node(arr[idx])
    node.left, idx = deserialize_tree(arr, idx + 1)
    node.right, idx = deserialize_tree(arr, idx)
    return node, idx
示例#14
0
def recurse(left, right):
    global head
    if left > right:
        return None
    mid = (left + right) // 2
    left_root = recurse(left, mid - 1)
    node = BinaryTreeNode(head.value)
    node.left = left_root
    # Maintain the invariant
    head = head.next
    node.right = recurse(mid + 1, right)
    return node
示例#15
0
def main():
    root = Node(20)
    root.left = Node(8)
    root.left.left = Node(4)
    root.left.right = Node(12)
    root.left.right.left = Node(10)
    root.left.right.right = Node(14)
    root.right = Node(22)
    root.right.right = Node(25)
    boundary = get_boundary_traversal(root)
    boundary = [node.data for node in boundary]
    print(boundary)
def deserialize(in_order, pre_order, in_start, in_end):
    if deserialize.in_order_index_map is None:
        build_reverse_map(in_order)
    if in_start > in_end:
        return None
    root = Node(pre_order[deserialize.pre_index])
    deserialize.pre_index += 1
    if in_start == in_end:
        return root
    root_in_index = deserialize.in_order_index_map[root.data]
    root.left = deserialize(in_order, pre_order, in_start, root_in_index - 1)
    root.right = deserialize(in_order, pre_order, root_in_index + 1, in_end)
    return root
示例#17
0
def main():
    root = BinaryTreeNode(1)

    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)

    root.left.left = BinaryTreeNode(4)
    root.left.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(6)
    root.right.right = BinaryTreeNode(7)
    root.left.left.right = BinaryTreeNode(101)
    root.right.left.left = BinaryTreeNode(102)
    print(Tree(root))
    print(get_diameter(root))
示例#18
0
def main():
    root = BinaryTreeNode(1)

    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)

    root.left.left = BinaryTreeNode(4)
    root.left.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(6)
    root.right.right = BinaryTreeNode(7)
    root.left.left.right = BinaryTreeNode(101)
    root.right.left.left = BinaryTreeNode(102)
    print(Tree(root))
    print(are_isomorphic(root, root))
示例#19
0
def main():
    root = BinaryTreeNode(1)

    root.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(3)

    root.left.left = BinaryTreeNode(4)
    root.left.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(6)
    root.right.right = BinaryTreeNode(7)
    root.left.left.right = BinaryTreeNode(101)
    root.right.left.left = BinaryTreeNode(102)
    print(Tree(root))
    mirror(root)
    print(Tree(root))
示例#20
0
def main():
    """
    Constructed binary tree is
                1
            / \
            2	 3
        / \
        4	 5
    """
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    ans = morris_inorder(root)
    print(ans)
示例#21
0
def 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)
    root.left.left.right = Node(101)
    root.right.left.left = Node(102)

    print(Tree(root))
    root2 = root.left.left
    ans = is_subtree(root, root2)
    print(ans)
示例#22
0
def 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)
    root.left.left.right = Node(101)
    root.right.left.left = Node(102)

    print(Tree(root))
    arr = []
    serialize_tree(root, arr)
    print(arr)
    root, idx = deserialize_tree(arr)
    print(Tree(root))
示例#23
0
def tree_to_dll(root: Node):
    """
    Returns (head, tail) of the dll.
    First convert left subtree, then right.
    Then connect head to both of them.
    """
    if root is None:
        return None, None
    left_head, left_tail = tree_to_dll(root.left)
    right_head, right_tail = tree_to_dll(root.right)
    if left_tail is not None:
        left_tail.right = root
    root.left = left_tail
    if right_head is not None:
        right_head.left = root
    root.right = right_head
    head = left_head
    if head is None:
        head = root
    tail = right_tail
    if tail is None:
        tail = root
    return head, tail
示例#24
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    print(Tree(root))
示例#25
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    print(Tree(root))
    print((get_lca(root, 20, 10)))