关于二叉树的按层打印
来源:互联网 发布:星星点灯 知乎 编辑:程序博客网 时间:2024/05/08 00:28
本文的思路来自于牛客网左程云大大的二叉树视频
视频地址
http://www.nowcoder.com/courses/1/1/1
题目要求如下:
至于算法分析,大家看视频吧,左老大讲的肯定比我清楚,下面是我实现的代码
视频地址
http://www.nowcoder.com/courses/1/1/1
题目要求如下:
至于算法分析,大家看视频吧,左老大讲的肯定比我清楚,下面是我实现的代码
class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}public class Solution {public static void main(String[] args) {TreeNode t1=new TreeNode(1);TreeNode t2=new TreeNode(2);TreeNode t3=new TreeNode(3);TreeNode t4=new TreeNode(4);TreeNode t5=new TreeNode(5);TreeNode t6=new TreeNode(6);TreeNode t7=new TreeNode(7);TreeNode t8=new TreeNode(8);t1.left=t2; t1.right=t3;t2.left=t4; t3.left=t5; t3.right=t6;t5.left=t7; t5.right=t8;new Solution().printFromTopToBottom(t1);} public ArrayList<Integer> printFromTopToBottom(TreeNode root) { ArrayList<Integer> list=new ArrayList<Integer>(); Queue <TreeNode> queue=new ArrayBlockingQueue<>(100); TreeNode last=root; //当前行的最后节点 TreeNode nLast=root; //下一行的最右节点 queue.add(root); while (!queue.isEmpty()) {TreeNode out=queue.poll();System.out.print(out.val+" "); list.add(out.val);if (out.left!=null) {queue.add(out.left);nLast=out.left;}if (out.right!=null) {queue.add(out.right);nLast=out.right;}if (out==last) {System.out.println("");last=nLast;}} return list; }}
0 0
- 关于二叉树的按层打印
- 二叉树的按层打印和ZigZag打印
- 二叉树的按层打印与ZigZag打印
- 按层打印二叉树
- 按层打印二叉树
- 按层打印二叉树
- 按层打印二叉树
- 按层打印二叉树
- 按层打印二叉树
- 二叉树问题---二叉树的按层打印和ZigZag打印
- 按层打印二叉树的节点【层次遍历变形】
- 算法实现-二叉树的按层打印
- 二叉树的遍历以及按层打印
- 打印二叉树的某一层
- 二叉树的层序打印
- [剑指offer]按层打印二叉树
- 从上到下按层打印二叉树
- 【36】按层打印二叉树
- 关于C++指向函数的指针 的理解
- 赛马问题
- Java的优点以及与C++的异同
- 待解决问题
- javascript中设置cookie
- 关于二叉树的按层打印
- [编程题] LeetCode上的Tree类型的题目
- USACO 1.1.4 - Broken Necklace(DP)
- hadoop2.x_博客推荐
- 数字电视传输流PCR抖动分析
- ARM平台下无线网卡的配置
- android shape的使用
- The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path
- 为什么要走嵌入式(这里是偏向软件的嵌入式)