在不分行的基础上要在遍历的同时要计算清楚当前层有多少个节点并把他们统一到一个list里
添加nodecount和nextnodecount
` public List[HTML_REMOVED]> printFromTopToBottom(TreeNode root) {
List[HTML_REMOVED]> res= new ArrayList<>();
if(root==null){
return res;
}
Queue[HTML_REMOVED] queue = new LinkedList<>();
queue.offer(root);
TreeNode head = null;
int nextnodecount = 1;
int nodecount = 0;
while(!queue.isEmpty()){
nodecount = nextnodecount;//把之前的next给现在的
nextnodecount = 0;//从新开始计数
List[HTML_REMOVED] list = new ArrayList<>();
for(int i = 1;i<=nodecount;i){
head = queue.poll();
list.add(head.val);
if(head.left!=null){
queue.offer(head.left);
nextnodecount;
}
if(head.right!=null){
queue.offer(head.right);
nextnodecount++;
}
}
res.add(list);
}
return res;
}
`