Creating a list of list after traversing binary search tree in level order
Nicely done. I especially like how you used the size of the queue to know how many items are there on the level. I have only minor improvement ideas.



You can simplify this declaration:


List&lt;List&lt;Integer&gt;&gt; list = new LinkedList&lt;List&lt;Integer&gt;&gt;();



Using the diamond operator &lt;&gt;, like you already did at other places:

List&lt;List&lt;Integer&gt;&gt; list = new LinkedList&lt;&gt;();




The code is deeply indented. You could reduce the indent level by eliminating the else branch of the outermost condition:

    if (root == null) {
        return list;
    }
    Queue&lt;TreeNode&gt; q = new LinkedList&lt;&gt;();
    q.add(root);
    // ...




The while loop could be written as a for loop. The benefit is that it will be harder to forget to decrement size.



Lastly, some of the names could be better.


Instead of "layers", I propose singular "layer"
Instead of "size", I propose "count"