Created
January 4, 2017 00:55
-
-
Save liondancer/1d5707495b2a024ae6ee010a1881c27e to your computer and use it in GitHub Desktop.
Print by level order
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 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