Creating a list of list after traversing binary search tree in level order Here is my code. 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
 public List&lt;List&lt;Integer&gt;&gt; levelOrder(TreeNode root) {
        List&lt;List&lt;Integer&gt;&gt; list = new LinkedList&lt;List&lt;Integer&gt;&gt;();
        if (root == null) {
            return list;
        } else {
            Queue&lt;TreeNode&gt; q = new LinkedList&lt;&gt;();
            q.add(root);
            while (!q.isEmpty()) {
                int size = q.size();
                LinkedList&lt;Integer&gt; layers = new LinkedList&lt;&gt;();
                while (size != 0) {
                    TreeNode current = q.poll();
                    layers.add(current.val);
                    if (current.left != null) {
                        q.add(current.left);
                    }
                    if (current.right != null) {
                        q.add(current.right);
                    }
                    size--;
                }

                list.add(layers);

            }
        }
        return list;

    }

}


Please let me know if any improvement/correction.
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"