Skip to content

Instantly share code, notes, and snippets.

@vigack
Created February 20, 2018 15:20
Show Gist options
  • Save vigack/51a4c5eb2d07fd6b077c1e47e259c279 to your computer and use it in GitHub Desktop.
Save vigack/51a4c5eb2d07fd6b077c1e47e259c279 to your computer and use it in GitHub Desktop.
Level order tree node traversal
def travel(root):
levels, level = [], [root]
while root and level:
levels.append([n.val for n in level])
level = [k for n in level for k in (n.left, n.right) if k]
return levels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment