Skip to content

Instantly share code, notes, and snippets.

@liondancer
Created January 4, 2017 00:55
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 liondancer/1d5707495b2a024ae6ee010a1881c27e to your computer and use it in GitHub Desktop.
Save liondancer/1d5707495b2a024ae6ee010a1881c27e to your computer and use it in GitHub Desktop.
Print by level order
def levelOrder(root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root:
queue = [root]
res = []
while queue:
tmp = []
next = []
for node in queue:
tmp.append(node.val)
if node.left:
next.append(node.left)
if node.right:
next.append(node.right)
res.append(tmp)
queue = next
return res
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment