7
/ \
1 2
/ \ /
0 3 4
\
2
7
1 2
0 3 4
2
def traverse_tree(node):
q = []
last_level = 0
current_level = 0
q.append([node, current_level])
while len(q) > 0:
node, current_level = q.pop()
if current_level != last_level:
last_level = current_level
print "\n"
print node + " "
left_child = node.left
right_child = node.right
if left_child not NULL:
q.append([left_child, current_level + 1])
if right_child not NULL:
q.append([right_child, current_level + 1])
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment