【剑指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 ...
随机推荐
- MPlayer参数使用介绍(部分)(中文)
文件太长,就不全部转载了,以下是某MPlayer网站的全部详细介绍: http://www.mplayer.org.cn/help_zh.html#%E7%A8%8B%E5%BA%8F%E9%94%9 ...
- .NET异步程序设计之任务并行库
目录 1.简介 2.Parallel类 2.0 Parallel类简介 2.1 Parallel.For() 2.2 Parallel.ForEach() 2.3 Parallel.Invoke() ...
- MySQL中遍历查询结果的常用API(c)
本中所使用的table: MySQL中的错误处理函数 unsigned int mysql_errno(MYSQL *mysql) const char *mysql_error(MYSQL *mys ...
- [Effective Java 读书笔记] 第三章类和接口 第十六条
第十六条 复合优先于继承 如果不确定B和A的关系是,is-a的关系,B确实也是A,那么久不应该使用B继承A,否则会暴露实现细节, 你的实现都会限制在原始的实现上. 书中举的第一个例子,实现了一个类ex ...
- Postman之命令测试
前言 今天我们来学习一下Postman的命令行测试 1.先安装node.js ,https://nodejs.org/en/#home-downloadhead 2.安装cnpm npm instal ...
- hadoop local、伪分布 模式
local模式: 把安装包 解压到 /usr/local 下 [hadoop@master local]$ sudo tar -zxvf hadoop-2.7.3.tar.gz 创建一个软连接 ...
- [LeetCode] [链表] 相关题目总结
刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...
- Lambda如何实现条件去重distinct List,如何实现条件分组groupBy List
条件去重 我们知道, Java8 lambda自带的去重为 distinct 方法, 但是只能过滤整体对象, 不能实现对象里的某个值进行判定去重, 比如: List<Integer> nu ...
- C# 中Trim()、TrimStart()、TrimEnd()、ToUpper()、ToLower()的用法
Trim():删除字符串头部及尾部出现的空格,删除的过程为从外到内,直到碰到一个非空格的字符为止,所以不管前后有多少个连续的空格都会被删除掉. TrimStart():只删除字符串的头部的空格. Tr ...
- 解决egg-mysql连接数据库报错问题
遇到这个问题,我在网上找了好多资料,最终于解决了!!!★,°:.☆( ̄▽ ̄)/$:.°★ . 我遇到的问题是这样的:链接mysql完全按照官网上做的,但是在yarn dev 时就是一直报错,错误我就不 ...