题目描述

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

题解一: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. C语言I作业1

    1 你对软件工程专业或计算机科学与技术专业了解是怎样的? 软件工程顾名思义就是工程化的方法生产软件的一门学科.涉及到程序设计语言,数据库,软件开发工具,系统平台,标准,设计模式等方面. 2 你了解c语 ...

  2. c++中各类型数据占据的字节长度

    c++中各种类型数据类型占据字节长度 首先罗列一下C++中的数据类型都有哪些: 1.整形:int.long 2.字符型:char.wchar_t 3.布尔型:bool 4.浮点型:float.doub ...

  3. Visual Studio 2012 出现关于ActivityLog.xml错误的解决方案

    由sp1升级sp2后出现的错误. devenv.exe /safemode启动下,就可以了 命令列參數 描述 /Command (devenv.exe) 啟動 IDE 並執行指定的命令. /Debug ...

  4. C2440 “初始化”: 无法从“std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>”转换为“std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>”

    错误原因vs已经提醒的很清楚了:无法将const_iterator转换为iterator 我的出错代码是这样的 思考了很久,最后发现原来是因为将函数定义为const的缘故. 总结:当将函数定义为con ...

  5. 动态主机配置协议-DHCP

    一.DHCP 概述 当局域网中有大量的PC时.如果我们逐个为每台PC去手动配置IP.那这就是一个吃力也未必讨好的办法 累死你 而DHCP 刚好可以解决这个问题.DHCP全称(动态主机配置协议).使用的 ...

  6. python学习(5)写一个二分算法的程序

    把之前学习的做一个小结.之前看二分查找法,只能是似而非地看懂大概.现在用这么多天的知识积累已经可以自己写了. 而且在算法书的基础上,把需要找的数字做一个人机互动操作. 另外,初步接触到了 __name ...

  7. 【5min+】 对象映射只有AutoMapper?试试Mapster

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  8. 申请Let’s Encrypt通配符HTTPS证书(certbot ACME v2版)

    1.获取certbot-auto# 下载 # 下载 wget https://dl.eff.org/certbot-auto # 设为可执行权限 chmod a+x certbot-auto 2.开始 ...

  9. .NET Core之单元测试(一):入门

    目录 什么是单元测试 .NET Core中的测试框架 一个最基础的单元测试 我们再看看上面的代码 什么是单元测试 单元测试是对软件中的最小可测试单元进行检查和验证.对于单元测试,要保证测试粒度足够小, ...

  10. 20200221--python学习第14天

    今日内容 带参数的装饰器:flash框架+django缓存+写装饰器实现被装饰的函数要执行N次 模块: os sys time datetime和timezone[了解] 内容回顾与补充 1.函数 写 ...