import java.util.ArrayList;
import java.util.LinkedList;
/**
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) {
//定义一个辅助队列
LinkedList<TreeNode> queue=new LinkedList<>();
//存储一层的打印结果
ArrayList<Integer> resultCur=new ArrayList<Integer>();
if(root==null){
return resultCur;
}
//定义两个变量last和nlast,分别记录当前行的最右节点和下一行的最右节点
TreeNode last=root;
TreeNode nlast=root;
TreeNode cur=root;
queue.add(root);
while(queue.size()!=0){
cur=queue.poll();
resultCur.add(cur.val);
if(cur.left!=null){
queue.add(cur.left);
nlast=cur.left;
}
if(cur.right!=null){
queue.add(cur.right);
nlast=cur.right;
}
if(cur==last){
last=nlast;
}
}
return resultCur;
}
}

  

最新文章

  1. 关于本月第一天,本月最后一天的SQL代码
  2. 用自己赚的钱第一次坐飞机 那feel倍儿爽
  3. MATLAB-octave中3维向量场图的可视化
  4. Python 学习笔记(2) - 基本概念、运算符与表达式
  5. sql 语句左连接右连接小例子
  6. 返回表对象的方法之一--bulk collect into
  7. 浮点数运算的精度问题:以js语言为例
  8. 怎么样通过php使用html5实现多文件上传?(php多图上传)
  9. 关于 php 和 python 的浮点计算 0.1+0.2
  10. django -- Celery实现异步任务
  11. Django中简单添加HTML、css、js等文件(非正规添加,适合小白)
  12. SqlServer中的dbo是什么意思
  13. CentOS下用yum配置php+mysql+apache
  14. J2EE Oa项目上传服务器出现的乱码解决过程
  15. JavaScript for 循环累加 json 字符串
  16. select和epoll概念
  17. 【学习笔记】关于DOM4J:使用DOM4J解析XML文档
  18. 实现高可用-Keepalived
  19. Selenium2+python自动化41-绕过验证码(add_cookie)【转载】
  20. C++拷贝构造函数(深拷贝,浅拷贝)

热门文章

  1. vivado2013.4和modelsim联合仿真
  2. ffmpeg截图
  3. 解密SVM系列(二):SVM的理论基础(转载)
  4. Android备份和添加短信
  5. 关于JQ checkbox选择的问题
  6. SPSS统计功能与模块对照表
  7. Spring Cloud 微服务二:API网关spring cloud zuul
  8. Python 推导式、迭代器、生成器、模块和包
  9. 【BZOJ3743】[Coci2015]Kamp 树形DP
  10. QQpet exploratory park(DP)