【剑指Offer】61、把二叉树打印成多行
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
题解一: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、把二叉树打印成多行的更多相关文章
- 【剑指Offer】把二叉树打印成多行 解题报告(Python)
[剑指Offer]把二叉树打印成多行 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...
- Go语言实现:【剑指offer】把二叉树打印成多行
该题目来源于牛客网<剑指offer>专题. 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 需要分层,二维数组. Go语言实现: /** * Definition for ...
- 剑指offer系列33-----把二叉树打印成多行
[题目]从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 方法一:直接打印 package com.exe7.offer; import java.util.LinkedList; i ...
- 【剑指offer】把二叉树打印成多行,C++实现
原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 从上到下按层打印二叉树,同一层结点从左至右输出,每一层输出一行.例如:下面二叉树的 ...
- 剑指Offer 60. 把二叉树打印成多行 (二叉树)
题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目地址 https://www.nowcoder.com/practice/445c44d982d04483b04a54f ...
- [剑指Offer] 60.把二叉树打印成多行
题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. [思路]使用队列实现二叉树的层次遍历. /* struct TreeNode { int val; struct TreeN ...
- 剑指offer60:把二叉树打印成多行。上到下按层打印二叉树。
1 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 2 思路和方法 vector变量存储每一层的元素vector<vector<int> > ans ...
- 剑指Offer 61. 序列化二叉树 (二叉树)
题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 题目地址 https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84 ...
- [剑指Offer] 61.序列化二叉树
题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *r ...
- 剑指Offer:二叉树打印成多行【23】
剑指Offer:二叉树打印成多行[23] 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目分析 Java题解 package tree; import java.uti ...
随机推荐
- 《快乐编程大本营》java语言训练班-第4课:java流程控制
<快乐编程大本营>java语言训练班-第4课:java流程控制 第1节. 顺序执行语句 第2节. 条件分支语句:if条件语句 第3节. 条件分支语句:switch 条件语句 第4节. 条件 ...
- ros机器人之动作(二)
前面我们实现了动作的定义,接下来实现动作的功能 实现一个基本的动作服务器 准备好所需的动作定义后就可以开始编写代码了.动作和话题一样,都是使用回调机制,即回调函数会在收到消息时被唤醒和调用. 例:si ...
- permission denied (publickey)问题的解决和向github添加ssh key
使用ssh key这种方式进行clone ,pull github上面的项目,使用 git clone或者git pull origin master出现permission denied (publ ...
- tensorflow feed_dict()
import tensorflow as tf a=tf.Variable(100) b=tf.Variable(200) c=tf.Variable(300) update1=tf.assign(c ...
- 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Go语言实现:【剑指offer】滑动窗口的最大值
该题目来源于牛客网<剑指offer>专题. 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存 ...
- GitHub新手教学(从新手安装到初步使用)
版权声明:本文为博主原创文章,转载请标明出处! 博客地址:http://blog.csdn.net/qazwsxpcm https://blog.csdn.net/qazwsxpcm/article/ ...
- postman批量接口测试/批量导入/批量参数化简要全过程及遇到问题处理方法
简单说明下postman批量接口调用的过程及注意事项: 1.报文调试(建议先调通再批量执行,统筹安排时间) 2.参数化,例如: "address": "{{address ...
- Hibernate框架预览以及基础介绍
前言 从本节我们开始进入到对于Hibernate框架的学习,当前Hibernate框架还未正式发布6.0稳定版本,所以这里我们以5.4.12Final版本进行讲解. Hibernate框架 Hiber ...
- 方法中this指向的问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...