class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
if(root == null) return ans;
while(!queue.isEmpty()){
int sz = queue.size();
List<Integer> levels = new ArrayList<Integer>();
while(sz -- > 0){
TreeNode cn = queue.poll();
levels.add(cn.val);
if(cn.left != null) queue.add(cn.left);
if(cn.right != null) queue.add(cn.right);
}
ans.add(levels);
}
return ans;
}
}