root.left = left

    if root.right:
        right = convert_to_doubly_linked_list(root.right)

        while right.left:
            right = right.left

        root.right = right
        right.left = root

    return root


def print_list(root):
    if not root:
        return

    while root.left:
        root = root.left

    head = root

    while head:
        print head.data
        head = head.right


tree = binary_tree.construct_binary_tree()
root = convert_to_doubly_linked_list(tree.root)
print_list(root)
Пример #2
0
# -*- coding: UTF-8 -*-

# Program to find the diameter of a binary tree
# i.e. number of nodes in the longest path between two leaves in the tree

import binary_tree

def diameter(tree, node):

	if node is None:
		return 0

	lh = tree.height(node.left)
	rh = tree.height(node.right)

	return max(lh+rh+1, max(diameter(tree, node.left), diameter(tree, node.right)))


if __name__=="__main__":
	tree = binary_tree.construct_binary_tree()

	print diameter(tree, tree.root)
def find_level():
    tree = binary_tree.construct_binary_tree()
    level = 0
    print level_of_node(tree.root, 8, level + 1)
Пример #4
0
def find_level():
	tree = binary_tree.construct_binary_tree()
	level = 0
	print level_of_node(tree.root, 8, level+1)
Пример #5
0
# -*- coding: UTF-8 -*-

# Identical Trees

import binary_tree

def identical_trees(root1, root2):
	if not root1 and not root2:
		return True

	if not root1 or not root2:
		return False

	else:
		if root1.data == root2.data and identical_trees(root1.left, root2.left) and identical_trees(root1.right, root2.right):
			return True
		else:
			return False

tree1 = binary_tree.construct_binary_tree()
tree2 = binary_tree.construct_binary_tree()
print identical_trees(tree1.root, tree2.root)