public ArrayList<ArrayList<Integer>>levelOrder(TreeNode root){
if(root ==null)return new ArrayList<>();
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);while(!queue.isEmpty()){
ArrayList<Integer> temp = new ArrayList<>();//记录当前queue的大小,这几个值属于同一层
int count = queue.size();for(int i =0; i < count; i++){
//得到当前遍历结点
TreeNode treeNode = queue.poll();
temp.add(treeNode.val);//遍历子结点,添加到队列中if(treeNode.left !=null) queue.offer(treeNode.left);if(treeNode.right !=null) queue.offer(treeNode.right);}
result.add(temp);}return result;}}