题目描述
BFS
JAVA 代码
class Solution {
public List<Integer> printFromTopToBottom(TreeNode root) {
ArrayList<TreeNode> nodeList = new ArrayList<>();
if (root == null){
return new ArrayList<>();
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()){
TreeNode node = queue.poll();
nodeList.add(node);
if (node.left != null){
queue.add(node.left);
}
if (node.right != null){
queue.add(node.right);
}
}
return nodeList.stream().map((n) -> n.val).collect(Collectors.toList());
}
}