题目:不分行从上往下打印出二叉树的每个节点,同层节点从左至右打印。例如输入{8,6,10,#,#,2,1},如以下图中的示例二叉树,则依次打印8,6,10,2,1(空节点不打印,跳过),请你将打印的结果存放到一个数组里面,返回。
数据范围:
0<=节点总数<=1000
-1000<=节点值<=1000
示例1
输入:{8,6,10,#,#,2,1}
返回值:[8,6,10,2,1]
示例2
输入:{5,4,#,3,#,2,#,1}
返回值:[5,4,3,2,1]
import java.util.*;
/**
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<>();
if(root == null)
return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode head = queue.peek();
if(head.left != null)
queue.offer(head.left);
if(head.right != null)
queue.offer(head.right);
int curVal = queue.remove().val;
list.add(curVal);
}
return list;
}
}