# 102. Binary Tree Level Order Traversal

二叉树层序遍历 => BFS with queue

```python
class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        res = []
        if not root: return res
        
        queue = collections.deque([root])
        while queue:
            n_ = len(queue)
            curLevel = []
            for i in range(n_):
                curNode = queue.popleft()
                curLevel.append(curNode.val)
                if curNode.left: queue.append(curNode.left)
                if curNode.right: queue.append(curNode.right)
            res.append(curLevel)
            
        return res
```

或者套层序遍历的模版

```python
class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        res = []
        self.bfs(root, 0, res)
        return res
        
    def bfs(self, root, level, result):
        if not root: return
        if len(result) == level: result.append([])
        result[level].append(root.val)
        if root.left: self.bfs(root.left, level+1, result)
        if root.right: self.bfs(root.right, level+1, result)
```
