题目描述

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

题解一: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. NR / 5G - W-OFDM

  2. Python 模拟登录几种常见方法

    方法一:直接使用已知的cookie访问 优点: 简单,但需要先在浏览器登录 原理: 简单地说,cookie保存在发起请求的客户端中,服务器利用cookie来区分不同的客户端.因为http是一种无状态的 ...

  3. vue 过渡 & 动画

    过渡 & 动画 过渡动画 用css先定义好动画效果 .a-enter-active, .a-leave-active { transition: all 1.5s; } .a-enter, . ...

  4. PPT导出图片质量太差?简单操作直接导出印刷质地图片

    PPT导出图片质量太差?简单操作直接导出印刷质地图片    ​ PPT不仅可以用于展示文档,还可以用于简单图片合成处理,同时,PPT文档还可以全部导出为图片. 默认情况下,PPT导出的图片为96DPI ...

  5. Transformer 和 Transformer-XL——从基础框架理解BERT与XLNet

    目录写在前面1. Transformer1.1 从哪里来?1.2 有什么不同?1.2.1 Scaled Dot-Product Attention1.2.2 Multi-Head Attention1 ...

  6. yum 升级php版本

    centos默认安装的php都是 5.3的  ,现在需要 5.6以上的版本 手动安装比较麻烦,直接用yum升级了. 一.准备工作 首先检查当前php版本 #php -v 查看安装的php扩展包 #yu ...

  7. [Redis-CentOS7]Redis哈希操作(五)

    哈希相当于下面Python代码 { "person":{ "name": "peigy", "age": "1 ...

  8. jdk升级后Eclipse无法启动问题

    overview: 今日安装jdk11,设置好环境变量后,eclipse无法运行,由于项目依赖原因,不想更新eclipse的版本. 我的jdk是1.8,在将环境变量设回1.8后依然无法运行.在多次尝试 ...

  9. asp.net core 3.x Identity

    一.前言 这方面的资料很多,重复的写没必要,但是最近一直在学习身份验证和授权相关东东,为了成体系还是写一篇,主要是从概念上理解identity系统. 参考:https://www.cnblogs.co ...

  10. Java中,一个存在了十几年的bug...

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...