JZ32 从上往下打印二叉树
从上往下打印二叉树_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
public class Solution {
public ArrayList PrintFromTopToBottom(TreeNode root) {
ArrayList list=new ArrayList<>();
if(root==null){
return list;
}
Queue q=new LinkedList<>();
q.offer(root);
while(q.isEmpty()==false){
TreeNode cur=q.poll();
list.add(cur.val);
if(cur.left!=null){
q.offer(cur.left);
}
if(cur.right!=null){
q.offer(cur.right);
}
}
return list;
}
}



