Created
November 7, 2013 21:13
-
-
Save labianchin/7361957 to your computer and use it in GitHub Desktop.
Print a binary tree of integers in level order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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