题目描述

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

题解一:BFS
 public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<>();
         LinkedList<TreeNode> queue = new LinkedList<>();
         if(pRoot==null){
             return res;
         }
         queue.offer(pRoot);
         while (queue.size()!=0){
             int size=queue.size();
             ArrayList<Integer> list = new ArrayList<>();
             for(int i=0;i<size;i++) {
                 TreeNode node = queue.poll();
                 if(node==null){
                     continue;
                 }
                 list.add(node.val);
                 queue.offer(node.left);
                 queue.offer(node.right);
             }
             if(list.size()!=0){
                 res.add(list);
             }
         }
         return res;
     }
题解二:递归
 public static ArrayList<ArrayList<Integer>> Print01(TreeNode pRoot) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<>();
         dept(pRoot,1,res);
         return res;
     }
     public static void dept(TreeNode root,int depth,ArrayList<ArrayList<Integer>> res){
         if(root==null){
             return;
         }
         //判断是否进入到下一层
         if(depth>res.size()){
             res.add(new ArrayList<Integer>());
         }
         //树的每一层都是一个list,将节点的值放入新创建的一层的list中
         res.get(depth-1).add(root.val);
         dept(root.left,depth+1,res);
         dept(root.right,depth+1,res);
     }

初始化树:

 public static class TreeNode{
         int val=0;
         TreeNode left=null;
         TreeNode right=null;
         public TreeNode(int val){
             this.val=val;
         }
     }
  private static List<TreeNode> nodeList = null;
     public static TreeNode createBinTree(int[] array) {
         nodeList=new LinkedList<TreeNode>();
         // 将一个数组的值依次转换为TreeNode节点
         for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
             nodeList.add(new TreeNode(array[nodeIndex]));
         }
         // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
         for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
             // 左孩子
             nodeList.get(parentIndex).left = nodeList
                     .get(parentIndex * 2 + 1);
             // 右孩子
             nodeList.get(parentIndex).right = nodeList
                     .get(parentIndex * 2 + 2);
         }
         // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
         int lastParentIndex = array.length / 2 - 1;
         // 左孩子
         nodeList.get(lastParentIndex).left = nodeList
                 .get(lastParentIndex * 2 + 1);
         // 右孩子,如果数组的长度为奇数才建立右孩子
         if (array.length % 2 == 1) {
             nodeList.get(lastParentIndex).right = nodeList
                     .get(lastParentIndex * 2 + 2);
         }
         return nodeList.get(0);
     }

测试:

 public static void main(String[] args) {
         int[] tree={8,6,10,5,7,9,11};
         TreeNode rootNode = createBinTree(tree);
         ArrayList<ArrayList<Integer>> lists = Print(rootNode);
         for (ArrayList<Integer> list : lists) {
             System.out.println(list);
         }
     }
 输出:
 [8]
 [6, 10]
 [5, 7, 9, 11]

【剑指Offer】61、把二叉树打印成多行的更多相关文章

  1. 【剑指Offer】把二叉树打印成多行 解题报告(Python)

    [剑指Offer]把二叉树打印成多行 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...

  2. Go语言实现:【剑指offer】把二叉树打印成多行

    该题目来源于牛客网<剑指offer>专题. 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 需要分层,二维数组. Go语言实现: /** * Definition for ...

  3. 剑指offer系列33-----把二叉树打印成多行

    [题目]从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 方法一:直接打印 package com.exe7.offer; import java.util.LinkedList; i ...

  4. 【剑指offer】把二叉树打印成多行,C++实现

    原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目       从上到下按层打印二叉树,同一层结点从左至右输出,每一层输出一行.例如:下面二叉树的 ...

  5. 剑指Offer 60. 把二叉树打印成多行 (二叉树)

    题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目地址 https://www.nowcoder.com/practice/445c44d982d04483b04a54f ...

  6. [剑指Offer] 60.把二叉树打印成多行

    题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. [思路]使用队列实现二叉树的层次遍历. /* struct TreeNode { int val; struct TreeN ...

  7. 剑指offer60:把二叉树打印成多行。上到下按层打印二叉树。

    1 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 2 思路和方法 vector变量存储每一层的元素vector<vector<int> > ans ...

  8. 剑指Offer 61. 序列化二叉树 (二叉树)

    题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 题目地址 https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84 ...

  9. [剑指Offer] 61.序列化二叉树

    题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *r ...

  10. 剑指Offer:二叉树打印成多行【23】

    剑指Offer:二叉树打印成多行[23] 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目分析 Java题解 package tree; import java.uti ...

随机推荐

  1. win10系统安装VMware虚拟机软件以及linux系统

    一.安装VMware 1.在VMware官网下载VMware Workstation Pro 15.5.1 下载地址:https://my.vmware.com/cn/web/vmware/detai ...

  2. 动手学习pytorch——(2)softmax和分类模型

    内容太多,捡重要的讲. 在分类问题中,通常用离散的数值表示类别,这里存在两个问题.1.输出值的范围不确定,很难判断值的意义.2.真实标签是离散值,这些离散值与不确定的范围的输出值之间的误差难以衡量. ...

  3. 17-SSM中通过pagehelper分页的实现

    SSM中通过pagehelper分页的实现 1. 在SSM框架的基础上实现,导包 <!-- 分页 --> <dependency> <groupId>com.git ...

  4. 学习CSS之用CSS实现时钟效果

    一.机械时钟 1.最终效果 用 CSS 绘制的机械时钟效果如下: HTML 中代码结构为: <body>     <div class="clock">   ...

  5. df du 文件空间管理 命令

     df  可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. du 可以查看文件及文件夹的大小.     df:常用   df -h    以易读形式显示 磁盘空间 linux ...

  6. 在windows系统安装nginx

    1.下载Nginx,链接:http://nginx.org/en/download.html 2.解压放到自己的磁盘,双击击运行nginx.exe,会有命令框一闪而过,在浏览器上面输入localhos ...

  7. File类和枚举

    java.io.File类:文件和目录路径名的抽象表示形式 File类常见构造方法: File(String pathname):通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例. ...

  8. PAT-1005 Spell It Right 解答(C/C++/Java/python)

    1.Description: Given a non-negative integer N, your task is to compute the sum of all the digits of  ...

  9. 基于BTrace监控调试Java代码

    BTrace是Java的一个动态代码追踪工具,通过编写btrace脚本,它可以动态的向目标应用程序的字节码注入追踪代码,通过修改字节码的方式,达到监控调试和定位问题的目的,是解决线上问题的利器. BT ...

  10. mysql必知必会--创建计算字段

    计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式.下面举 几个例子. * 如果想在一个字段中既显示公司名,又显示公司的地址,但这两 个信息一般包含在不同的表列中. * 城市.州和邮政编码存 ...