题目描述

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

题解一: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. 实验7:交换机IOS升级

    交换机IOS升级首先需要有IOS文件,如果没有备份原文件的话,可以找个同一版本的IOS来替代. 第一种方法:X-Modem 以前我曾经尝试过一种方法,就是当Flash被删除后,启动无法进入系统,可以用 ...

  2. Codeforces_834

    A.两个方向都判断. #include<bits/stdc++.h> using namespace std; string s1,s2; map<char,int> mp; ...

  3. lua学习之表达式篇

    表达式 表达式用于表达值 lua 中表达式可以为数字常量,自变字符串,变量,一元和二元操作符,函数调用.函数定义.table 构造式 算数操作符 一元操作符 -负号 二元操作符 -减号 / ^ % x ...

  4. 编写SQL语句(快速回顾)

    注:源自于<Java程序员面试秘笈>! 1.创建数据库MYDB create database MYDB 2.创建学生表student (sno,sname,ssex,sage,sclas ...

  5. qt creator源码全方面分析(2-10)

    目录 Creating Plugins Creating Plugins Qt Creator的核心是一个插件加载程序,加载并运行一组插件,实际上是这些插件提供了您从Qt Creator IDE中了解 ...

  6. ibtmp1文件过大

    有个数据库发现磁盘告警 已经100% 经过排查发现数据库的data目录下有个 ibtmp1是个什么东西呢?查看官方文档后发现 The temporary tablespace is a tablesp ...

  7. win10打开自带wifi热点共享

    win10打开自带wifi热点共享 第一步,打开网络和Internet设置 二. 找到移动热点

  8. BP神经网络拟合给定函数

    近期在准备美赛,因为比赛需要故重新安装了matlab,在里面想尝试一下神将网络工具箱.就找了一个看起来还挺赏心悦目的函数例子练练手: y=1+sin(1+pi*x/4) 针对这个函数,我们首先画出其在 ...

  9. Win环境下安装vue及运行vue开发的前端项目

    vue安装及配置 首先下载node.js要求版本在8.9以上        官网:https://nodejs.org/zh-cn/ 下载完可检查在windows任务命令行里输入node -v 使用淘 ...

  10. UML之二、建模元素(1)

    本章介绍UML建模元素 1:Stereotype-也被称为类型.构造型 UML里的元素扩展,简单来说其功能就是在已有的类型上添加一些标记,类似于打个戳,从而生成新的东西. 简单的说加一句话来更加清楚准 ...