1 import java.util.ArrayList; 2 import java.util.LinkedList; 3 import java.util.Queue; 4 /** 5 public class TreeNode { 6 int val = 0; 7 TreeNode left = null; 8 TreeNode right = null; 9 10 public TreeNode(int val) { 11 this.val = val; 12 13 } 14 15 } 16 */ 17 public class Solution { 18 public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { 19 20 ArrayList<Integer> list = new ArrayList<Integer>(); 21 22 Queue<TreeNode> queue = new LinkedList<TreeNode>(); 23 24 queue.offer(root); //根结点入queue 25 26 while(root != null){ 27 TreeNode node = queue.poll(); //结点出queue 28 list.add(node.val); 29 30 //结点的左右孩子入queue 31 if(root.left != null){ 32 queue.offer(root.left); 33 } 34 if(root.right != null){ 35 queue.offer(root.right); 36 } 37 38 root = queue.peek(); 39 } 40 41 return list; 42 43 } 44 }