tests/java/src/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.java
public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> results = new ArrayList<List<Integer>>();
        if (root == null) {
            return results;
        }

        ArrayList<Integer> values = new ArrayList<Integer>();
        Queue<TreeNode> q = new LinkedList<TreeNode>();

        q.offer(root);
        q.offer(null);

        while (q.size() > 0) {
            TreeNode node = q.poll();
            // null node used as a separator of every level
            if (node == null) {
                results.add(new ArrayList<Integer>(values));
                values.clear();
                if (q.size() == 0) {
                    break;
                }
                q.offer(null);
                continue;
            }
            values.add(node.val);
            if (node.left != null) {
                q.offer(node.left);
            }
            if (node.right != null) {
                q.offer(node.right);
            }
        }

        return results;
    }
/**
     * @param root: The root of binary tree.
     * @return: Level order a list of lists of integer
     */
@param root: The root of binary tree.
@return: Level order a list of lists of integer
@param root : The root of binary tree . @return : Level order a list of lists of integer
public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> results = new ArrayList<List<Integer>>();
        if (root == null) {
            return results;
        }
        ArrayList<Integer> values = new ArrayList<Integer>();
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(root);
        q.offer(null);
        while (q.size() > 0) {
            TreeNode node = q.poll();
            if (node == null) {
                results.add(new ArrayList<Integer>(values));
                values.clear();
                if (q.size() == 0) {
                    break;
                }
                q.offer(null);
                continue;
            }
            values.add(node.val);
            if (node.left != null) {
                q.offer(node.left);
            }
            if (node.right != null) {
                q.offer(node.right);
            }
        }
        return results;
    }
public List < List < Integer > > levelOrder ( TreeNode root ) { List < List < Integer > > results = new ArrayList < List < Integer > > ( ) ; if ( root == null ) { return results ; } ArrayList < Integer > values = new ArrayList < Integer > ( ) ; Queue < TreeNode > q = new LinkedList < TreeNode > ( ) ; q . offer ( root ) ; q . offer ( null ) ; while ( q . size ( ) > 0 ) { TreeNode node = q . poll ( ) ; if ( node == null ) { results . add ( new ArrayList < Integer > ( values ) ) ; values . clear ( ) ; if ( q . size ( ) == 0 ) { break ; } q . offer ( null ) ; continue ; } values . add ( node . val ) ; if ( node . left != null ) { q . offer ( node . left ) ; } if ( node . right != null ) { q . offer ( node . right ) ; } } return results ; }
@param root: The root of binary tree.
@param root : The root of binary tree .
// null node used as a separator of every level