剑指offer系列33-----把二叉树打印成多行
【题目】从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
方法一:直接打印
package com.exe7.offer; import java.util.LinkedList;
import java.util.Queue; /**
* 【题目】从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
* @author WGS
*
*/
public class PrintBiTreeFromTopToBottom {
static class TreeNode{
int val=0;
TreeNode left=null;
TreeNode right=null;
public TreeNode(int val){
this.val=val;
}
} public void printBiTreeFromTopToBottom(TreeNode headNode){
if(headNode==null) return ;
int nextLevelNodes=0;
int toBoPrint=1;//设置个初始值
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(headNode);
while(!queue.isEmpty()){
//1 打印
TreeNode curNode=queue.poll();
System.out.print(curNode.val+" "); //2 添加左右结点
if(curNode.left!=null){
queue.add(curNode.left);
nextLevelNodes++;//下层要打印的结点数+1
}
if(curNode.right!=null){
queue.add(curNode.right);
nextLevelNodes++;//下层要打印的结点数+1
}
//左右结点添加完后即打印完本层一个结点值,自动减1(要判断本层是否还有别的值)
--toBoPrint;
if(toBoPrint==0){//本层打印完
System.out.println();//换行
toBoPrint=nextLevelNodes;//下层要打印的结点数
nextLevelNodes=0;//清0
}
} }
public static void main(String[] args) {
PrintBiTreeFromTopToBottom p=new PrintBiTreeFromTopToBottom();
TreeNode root = new TreeNode(8);
TreeNode node1 = new TreeNode(6);
TreeNode node2 = new TreeNode(10);
TreeNode node3 = new TreeNode(5);
TreeNode node4 = new TreeNode(7);
TreeNode node5 = new TreeNode(9);
TreeNode node6 = new TreeNode(11); root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
node2.left = node5;
node2.right = node6; p.printBiTreeFromTopToBottom(root);
} }
方法二:使用博主的方法(面试时候推荐此种方法,个别代码差异,思想一样)
package com.exe7.offer; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue; /**方法二:使用博主的方法(面试时候推荐此种方法,个别代码差异,思想一样)
* 【题目】从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
* @author WGS
*
*/
public class PrintBiTreeFromTopToBottom2 {
static class TreeNode{
int val=0;
TreeNode left=null;
TreeNode right=null;
public TreeNode(int val){
this.val=val;
}
} public ArrayList<ArrayList<Integer>> printBiTreeFromTopToBottom(TreeNode headNode){
ArrayList<ArrayList<Integer>> list=new ArrayList<>();//接收总共行的结点
ArrayList<Integer> nodeList=new ArrayList<>();//接收每行结点
if(headNode==null) return list;
int nextLevelNodes=0;//下层要打印的结点数
int toBoPrint=1;//设置个初始值
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(headNode);
while(!queue.isEmpty()){
//1 打印
TreeNode curNode=queue.poll();
//System.out.print(curNode.val+" ");
nodeList.add(curNode.val);
//2 添加左右结点
if(curNode.left!=null){
queue.add(curNode.left);
nextLevelNodes++;//下层要打印的结点数+1
}
if(curNode.right!=null){
queue.add(curNode.right);
nextLevelNodes++;//下层要打印的结点数+1
}
//左右结点添加完后即打印完本层一个结点值,自动减1(要判断本层是否还有别的值)
--toBoPrint;
if(toBoPrint==0){//本层打印完
//System.out.println();//换行
list.add(nodeList);
toBoPrint=nextLevelNodes;//下层要打印的结点数
nextLevelNodes=0;//清0
nodeList=new ArrayList<>();
}
}
return list; }
public static void main(String[] args) {
PrintBiTreeFromTopToBottom2 p=new PrintBiTreeFromTopToBottom2();
TreeNode root = new TreeNode(8);
TreeNode node1 = new TreeNode(6);
TreeNode node2 = new TreeNode(10);
TreeNode node3 = new TreeNode(5);
TreeNode node4 = new TreeNode(7);
TreeNode node5 = new TreeNode(9);
TreeNode node6 = new TreeNode(11); root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
node2.left = node5;
node2.right = node6; ArrayList<ArrayList<Integer>> getList=p.printBiTreeFromTopToBottom(root);
for (ArrayList<Integer> arrayList : getList) {
System.out.println(arrayList);
}
} }
剑指offer系列33-----把二叉树打印成多行的更多相关文章
- 剑指offer系列32-----对称二叉树的判断
[题目]请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. package com.exe7.offer; /** * [题目]请实现一个函 ...
- 剑指offer系列53---字符串转化成整数
[题目]将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数 * []整数(32位)最大值:0X7fff ffff( 0111 1111 1111 1111 1111 1111 1111 1 ...
- 剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计算法找其中的一个数
题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 2 4 9 12 4 7 10 13 6 8 11 ...
- 剑指Offer:二叉树打印成多行【23】
剑指Offer:二叉树打印成多行[23] 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目分析 Java题解 package tree; import java.uti ...
- 把二叉树打印成多行 牛客网 剑指Offer
把二叉树打印成多行 牛客网 剑指Offer 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行 # class TreeNode: # def __init__(self, x) ...
- 【剑指Offer】把二叉树打印成多行 解题报告(Python)
[剑指Offer]把二叉树打印成多行 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...
- 剑指Offer - 九度1368 - 二叉树中和为某一值的路径
剑指Offer - 九度1368 - 二叉树中和为某一值的路径2013-11-23 03:46 题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结 ...
- 干货 | 剑指offer系列文章汇总
下面是名企面试中经常会出现的面试题目,大家可以戳相应的题目查看题目细节,其答案会在紧接着的后一篇中出现 剑指offer系列 始 剑指offer—灯管问题(1) 剑指offer—10人电梯(2) ...
- 《剑指offer》从尾到头打印链表
本题来自<剑指offer> 从尾到头打印链表 题目: 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 思路: 方案一:首先遍历到尾部,然后从尾部进行到头值进行操作,后进先 ...
- 剑指Offer - 九度1521 - 二叉树的镜像
剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...
随机推荐
- 五 Servlet 技术
一 Servlet 基础 1. 定义相关: a) 是运行在 Web 服务器上得 Java 小程序 b) 只将处理结果返回给客户 c) 是实现接口 Servlet 的 java 类,能被服务器调用. d ...
- Xcode编译异常和警告汇总(持续更新中)
1.Method definition for 'xxx' not found xxx的方法没有实现 出现原因.h声明了xxx方法但是.m没有实现xxx方法 解决方法:在类的.m文件实现xxx方法 ...
- 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- csu 1604 SunnyPig (bfs)
Description SunnyPig is a pig who is much cleverer than any other pigs in the pigpen. One sunny morn ...
- HTML <area> 标签 带有可点击区域的图像映射(图像映射指的是带有可点击区域的图像)
例子: <img src="planets.gif" width="145" height="126" alt="Plane ...
- C++ Primer : 第十二章 : 动态内存之unique_ptr和weak_ptr
unique_ptr 一个unique_ptr拥有它所管理的对象,与shared_ptr不同,unique_ptr指向的对象只能有一个用户.当unique_ptr被销毁后,它所指向的对象也被销毁. 定 ...
- SQLServer如何用T—SQL命令查询一个数据库中有哪些表
1.查询SQL中的所有表: Select TABLE_NAME FROM 数据库名称.INFORMATION_SCHEMA.TABLES Where TABLE_TYPE='BASE TABLE' 执 ...
- linux下遍历目录(转-在于思考)
遍历目录的主要思想 由于目录就是一颗树,所以遍历目录就转换为遍历一棵树.谈到树的遍历就再熟悉不过了,有树的前序.层次和后序遍历,我使用的是前序遍历,后序遍历和前序遍历本质上一样,而层次遍历要比前两个麻 ...
- kuangbin_ShortPath O (LightOJ 1074)
这是什么鬼OJ啊都没见过害的我还交错语言CE了一发摔 想着懒得重写了直接把上一题的dij改了改就交了 然后RE 反应过来这题有负环 想着怎么标记负环同时不直接结束spfa 看了别人的代码感叹了一下我还 ...
- 论文阅读之 DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation
DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation Xia ...