博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从上向下打印二叉树
阅读量:4541 次
发布时间:2019-06-08

本文共 1165 字,大约阅读时间需要 3 分钟。

1 import java.util.ArrayList; 2 import java.util.LinkedList; 3 import java.util.Queue; 4 /** 5 public class TreeNode { 6     int val = 0; 7     TreeNode left = null; 8     TreeNode right = null; 9 10     public TreeNode(int val) {11         this.val = val;12 13     }14 15 }16 */17 public class Solution {18     public ArrayList
PrintFromTopToBottom(TreeNode root) {19 20 ArrayList
list = new ArrayList
();21 22 Queue
queue = new LinkedList
();23 24 queue.offer(root); //根结点入queue25 26 while(root != null){27 TreeNode node = queue.poll(); //结点出queue28 list.add(node.val);29 30 //结点的左右孩子入queue31 if(root.left != null){32 queue.offer(root.left);33 }34 if(root.right != null){35 queue.offer(root.right);36 }37 38 root = queue.peek();39 }40 41 return list;42 43 }44 }

 

转载于:https://www.cnblogs.com/jiangyi-uestc/p/5892905.html

你可能感兴趣的文章
【23.33%】【codeforces 664C】International Olympiad
查看>>
java-网络编程-使用URLDecoder和URLEncoder
查看>>
最短路之dijkstra算法
查看>>
SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成
查看>>
Lua语法基础(一)
查看>>
.Net Core2.*学习手册
查看>>
实验一、命令解释程序的编写实验
查看>>
2018年11月14日 学习字符串用法2
查看>>
2019年5月26日 re模块2
查看>>
Python学习笔记(一)——初学Python
查看>>
顺序表应用8:最大子段和之动态规划法(SDUT 3665)
查看>>
Python内置函数(52)——range
查看>>
正则表达式
查看>>
c# 执行 sql service 的存储过程
查看>>
《软件构架实践》读后感03
查看>>
jQuery入门(4)jQuery中的Ajax应用
查看>>
Java 发送http GET/POST请求
查看>>
索引之详解一
查看>>
Android Jetpack组件
查看>>
使用.NET Core 2.1的Azure WebJobs
查看>>