迭代法的话就是 用好 队列queue的poll offer
public static List<List<Integer>> levelOrder(TreeNode root){
ArrayList<List<Integer>> result = new ArrayList<>();
Deque que= new LinkedList();
if(root==null) return result;
TreeNode cur = root;
que.offer(cur);
while(!que.isEmpty()) {
ArrayList<Integer> subList = new ArrayList<>();
int len= que.size();// 本层长度固定的
for(;len>0;len--) {
TreeNode tmp = (TreeNode) que.poll();
subList.add(tmp.val);
if(tmp.left!=null) {
que.offer(tmp.left);
}
if(tmp.right!=null){
que.offer(tmp.right);
}
}
result.add(subList);
}
return result;
}