题目:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
是一个二叉树的层次遍历
import java.util.ArrayList;
/**
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){
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
if(root==null){
return list;
}
queue.add(root);
while(queue.size()!=0){
TreeNode temp = queue.remove(0);
if(temp.left!=null){
queue.add(temp.left);
}
if(temp.right!=null){
queue.add(temp.right);
}
list.add(temp.val);
}
return list;
}
}