public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList<Integer> ret = new ArrayList<>(); if(root==null) return ret; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(queue.size()>0){ int len=queue.size(); for(int i=0; i<len; i++){ TreeNode temp = queue.poll(); ret.add(temp.val); if(temp.left!=null) queue.offer(temp.left); if(temp.right!=null) queue.offer(temp.right); } } return ret; }
原文:https://www.cnblogs.com/zhwcs/p/10434587.html