题解 | #求二叉树的层序遍历#
求二叉树的层序遍历
http://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3
用deque数据结构
from collections import deque class Solution: def levelOrder(self , root ): # write code here if not root: return None q = deque([]) q.append(root) res = [] while q: size = len(q) temp = [] for i in range(size): curr = q.popleft() temp.append(curr.val) if curr.left: q.append(curr.left) if curr.right: q.append(curr.right) res.append(temp) return res