Ejemplo n.º 1
0
            q.put(node.left)

        if node.right:
            q.put(node.right)


if __name__ == '__main__':
    t = Node(1)
    t.left = Node(2)
    t.right = Node(3)
    t.left.left = Node(4)
    t.left.right = Node(5)
    iterative_mirror(t)
    print t, t.left, t.right, t.right.left, t.right.right


def mirror_n_nary_tree(root):
    root.children.reverse()

    for child in root.children:
        mirror_n_nary_tree(child)


if __name__ == '__main__':
    t = NNode(1)
    t.children = [NNode(i) for i in range(2, 6)]
    t.children[3].children = [NNode(6), NNode(7)]

    mirror_n_nary_tree(t)
    print t, t.children, t.children[0].children
Ejemplo n.º 2
0
    child_sizes = []
    for child in root.children:
        size = num_paths_to_remove_for_even_forest(child)
        child_sizes.append(size)

    for size in child_sizes:
        if size > 0 and size % 2 == 0:
            TO_REMOVE += 1

    return 1 + sum(child_sizes)


if __name__ == '__main__':
    print ''
    r = NNode(1)
    r.children = [NNode(2), NNode(3), NNode(4)]
    r.children[0].children = [NNode(5)]
    r.children[0].children[0].children = [NNode(9), NNode(10)]
    r.children[1].children = [NNode(6)]
    r.children[2].children = [NNode(7), NNode(8)]

    num_paths_to_remove_for_even_forest(r)
    print TO_REMOVE
"""
http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-heap/

Just one level order traversal of the given binary tree would be sufficient to check
both the properties of binary heap
To check Complete binary tree property: Just check no NON NULL value should proceed,
once any NULL value is inserted into the queue.
Checking the other property of binary heap is very easy, while inserting the