题目描述

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

题解一: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. Codeforces_822

    A.小的那个数的阶乘. #include<bits/stdc++.h> using namespace std; int a,b; int main() { ios::sync_with_ ...

  2. android studio闪退的原因

    可能是因为某个监听的问题,比如没有把Mainacticity中定义的button和布局文件中的按钮控件关联起来,就会出现这个问题

  3. 在form子句中使用子查询时的注意事项

    今天中午为了弄清这个问题,本人真的是头都搞大了!最后明白了一点,在from子句中使用子查询是,一定要将临时表的别名带上,否则会灰常痛苦!!!

  4. 题解 【[MdOI2020] Decrease】

    \[ \texttt{Preface} \] 感觉 C 比 B 还简单? \[ \texttt{Description} \] 给定一个 \(n×n\) 的矩阵,你可以进行若干次操作. 每次操作,你可 ...

  5. Altium Designer打印多块PCB到热转印纸上、拼板发给工厂

    接下来介绍的方法的优势有: 节省电脑性能(来自不同PCB文件的图案被放置时只在第1块会卡顿一小会儿,之后不再卡顿) 便于排版(拖放图案时绝不会改变图案内容,拖放图案时鼠标拖住的矩形框的尺寸与图案的尺寸 ...

  6. DOCKER 学习笔记9 Kubernetes (K8s) 弹性伸缩容器 下

    前言 从上一篇看来,我们已经对于Kubernetes ,通过minikube 建立集群,而后使用kubectl 进行交互,对Deployment 部署以及服务的暴露等.这节,将学习弹性的将服务部署到多 ...

  7. lwip nd没有实现ra,contik有参考

    lwip中关于nd的实现,没有路由器的功能,不能发送ra 在contiki中发现有nd发送ra的实现, contiki/core/net/ipv6/uip-ds6.c 在rs的接收处理中,发送soll ...

  8. docker device or resource busy

    docker-compose -f docker-compose.yml up -d  时候报错 device or resource busy 使用 docker-compose down 会导致一 ...

  9. AJAX传输图片文件

    AJAX传输 例:const xhr = new XMLHttpRequest(); // 此方法因为状态改变被调用多次,实测执行三次(1->2->4) xhr.onreadystatec ...

  10. [Python]获取字典所有值

    方法一:Key Value 直接获取 databases = {1: 'Student', 2: 'School'} for k,v in databases.items(): print(k,v) ...