Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 14, 2015 04:04
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 lettergram/6d92251cd7a96b6f9c8c to your computer and use it in GitHub Desktop.
Save lettergram/6d92251cd7a96b6f9c8c to your computer and use it in GitHub Desktop.
'''
Level Order Traversal
'''
def LevelOrder(self, root):
if root is None:
pass
else:
q = Queue.Queue()
currLevelCount = 1
nextLevelCount = 0
q.put(root)
while not q.empty():
root = q.get()
currLevelCount -= 1
if root is not None:
print(root.data),
q.put(root.left)
q.put(root.right)
nextLevelCount += 2
if currLevelCount is 0:
print ""
currLevelCount = nextLevelCount
nextLevelCount = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment