参考:
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/solution/er-cha-shu-de-ceng-ci-bian-li-by-leetcode/

package T102二叉树的层次遍历;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class Solution {

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) return result;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node == null) {
                    continue;
                }
                level.add(node.val);
                queue.add(node.left);
                queue.add(node.right);
            }
            if (level.size() != 0) {
                result.add(level);
            }
        }
        return result;
    }

}
```

from queue import Queue


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def levelOrder(self, root: TreeNode):
        queue = Queue()
        queue.put_nowait(root)
        orders = list()
        if root is None:
            return orders
        while queue.empty() is False:
            level_queue = Queue()
            level = list()
            while queue.empty() is False:
                cursor = queue.get_nowait()
                if cursor is None:
                    continue
                level.append(cursor.val)
                level_queue.put_nowait(cursor.left)
                level_queue.put_nowait(cursor.right)
            if level:
                orders.append(level)
            queue = level_queue
        return orders