Created
February 20, 2018 15:20
-
-
Save vigack/51a4c5eb2d07fd6b077c1e47e259c279 to your computer and use it in GitHub Desktop.
Level order tree node traversal
This file contains 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
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