import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
//定义一个辅助的队列
Queue<TreeNode> auxiliaryQueue=new LinkedList<TreeNode>();
ArrayList<Integer> result=new ArrayList<Integer>();
//判断边界条件
if(root==null){
return result;
}
//将头结点插入队列
auxiliaryQueue.offer(root);
while(!auxiliaryQueue.isEmpty()){
//定义一个指针始终等于队列的第一个节点
TreeNode currentRoot=auxiliaryQueue.peek();
result.add(currentRoot.val);
auxiliaryQueue.poll();
//判断队列的第一个节点是否有左右节点
if(currentRoot.left!=null){
auxiliaryQueue.offer(currentRoot.left);
}
if(currentRoot.right!=null){
auxiliaryQueue.offer(currentRoot.right);
}
}
return result;
}
}