剑指offer----从上到下打印二叉树----java实现
来源:互联网 发布:淘宝进货渠道有哪些 编辑:程序博客网 时间:2024/05/16 06:55
题目:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路:其实就是二叉树的层次遍历问题,借助于队列来实现对二叉树的层次遍历
代码:
import java.util.ArrayList;import java.util.LinkedList;/*class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class LevelTest{ public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {ArrayList<Integer> list = new ArrayList<Integer>(); if(root == null) return list; LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); TreeNode current = null; queue.offer(root); while(!queue.isEmpty()) { current = queue.poll();//出队当前根节点 list.add(current.val);//将根节点元素的值添加到list中 if(current.left != null) queue.offer(current.left); if(current.right != null) queue.offer(current.right); } return list; }}
0 0
- 剑指offer----从上到下打印二叉树----java实现
- 剑指offer 从上到下打印二叉树
- 《剑指Offer》面试题23:从上到下打印二叉树
- 剑指 offer:从上到下打印二叉树(层序遍历)
- 剑指offer系列之二十一:从上到下打印二叉树
- 剑指offer面试题23:从上到下打印二叉树
- 剑指offer——从上到下打印二叉树
- 剑指Offer系列-面试题23:从上到下打印二叉树
- 【剑指offer-解题系列(22)】从上到下打印二叉树
- 剑指offer 面试题23 从上到下打印二叉树
- 剑指offer——从上到下打印二叉树
- 【剑指offer】面试题32:从上到下打印二叉树
- [剑指offer]python从上到下打印二叉树【面试题9.15】
- 剑指offer之从上到下打印二叉树(Python)
- 从上到下打印二叉树
- 从上到下打印二叉树
- 从上到下打印二叉树
- 从上到下打印二叉树
- 开发时常用的数学函数
- java面向对象
- 最短路+记录路径+变形!!
- 为导航栏创建下拉菜单
- MATLAB“out of memory"的一点总结
- 剑指offer----从上到下打印二叉树----java实现
- 解决SQL Server 2008 R2安装过程中提示Could not open key的解决方法
- intellij引入jar包怎么加载jar包的源代码
- 编译内核是出现:arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments
- PAT L2-001. 紧急救援(扩展dijkstra)
- LIBSVM学习(三)数据格式
- svn:E200033:database is locked
- IDEA使用maven和tomcat搭建简单web案例
- 分布式文件存储 搭建FastDFS-5.0.5集群