#102.Binary Tree Level Order Traversal [LeetCode Grind 75 in Java]

[Problem Link] https://leetcode.com/problems/binary-tree-level-order-traversal/

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;

    }
}