diff = ldata + rdata - node.data
        if diff > 0 :
            node.data = node.data + diff
        elif diff < 0 :
            increment(node.left,-diff)

def increment(node,diff):
    if not node :
        return
    else :
        node.data = node.data + diff
        if node.left :
            increment(node.left,diff)
        elif node.right :
            increment(node.right,diff)

if __name__=='__main__':
    bt = BinaryTree()
    bt.add_node(50)
    bt.add_node(7)
    bt.add_node(2)
    bt.add_node(3)
    bt.add_node(5)
    bt.add_node(1)
    bt.add_node(30)

    convert(bt.root)
    bt.print_levelorder()

Пример #2
0
            node = que.get()
            if node.left :
                que.put(node.left)
            if node.right :
                que.put(node.right)

            node.left, node.right = node.right, node.left

def _create_mirror(node):
    """
    Recursive implementation
    :param node:
    :return:
    """
    if not node :
        return None
    else :
        _create_mirror(node.left)
        _create_mirror(node.right)
        node.left,node.right = node.right, node.left



if __name__=='__main__':
    tree = BinaryTree()
    for i in range(1,10):
        tree.add_node(i)
    create_mirror(tree.root)
    tree.print_levelorder()
    _create_mirror(tree.root)
    tree.print_levelorder()