---
# System prepended metadata

title: 0102. Binary Tree Level Order Traversal
tags: [Leetcode, BFS, Medium]

---

# 0102. Binary Tree Level Order Traversal
###### tags: `Leetcode` `Medium` `BFS`
Link: https://leetcode.com/problems/binary-tree-level-order-traversal/
## 思路
核心思想：BFS每遍历完一层之后，queue里面就只有这一层的node
## Code
```java=
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(root==null) return res;
        
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(root);
        while(!q.isEmpty()){
            List<Integer> level = new ArrayList<Integer>();
            int currSize = q.size();
            for(int i = 0;i < currSize;i++){
                TreeNode node = q.poll();
                level.add(node.val);
                if(node.left!=null){
                    q.offer(node.left);
                }
                if(node.right!=null){
                    q.offer(node.right);
                }
            }
            res.add(level);
        }
        return res;
    }
}
```