Skip to content

Instantly share code, notes, and snippets.

@labianchin
Created November 7, 2013 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save labianchin/7361957 to your computer and use it in GitHub Desktop.
Save labianchin/7361957 to your computer and use it in GitHub Desktop.
Print a binary tree of integers in level order.
#!/usr/bin/python
class Node:
def __init__(self, value, children=[]):
self.children = children
self.value = value
def tree_level_order(self):
level = [self]
while level:
print ' '.join([str(node.value) for node in level])
level = [child for node in level for child in node.children]
tree = Node(1, \
[Node(2, [Node(4)]),\
Node(3, [Node(5), Node(6)])])
tree.tree_level_order()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment