题解 | #求二叉树的层序遍历#python 11行代码
求二叉树的层序遍历
http://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3
class Solution:
def levelOrder(self , root ):
# write code here
if not root:return []
queue = [root]
res = []
while queue:
res.append([node.val for node in queue])
temp = []
for node in queue:
if node.left:temp.append(node.left)
if node.right:temp.append(node.right)
queue = temp
return res
查看20道真题和解析