剑指 Offer 32 - I. 从上到下打印二叉树
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回:
[3,9,20,15,7]
提示:
节点总数 <= 1000
思路
就是一个层序遍历
比较简单
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int[] levelOrder(TreeNode root) {
//边界值
if (root == null) return new int[]{};
//层序遍历树🌲
//建立一个队列
Queue<TreeNode> q = new LinkedList<TreeNode> ();
//将根节点入队
q.add(root);
//新建一个具有树节点个数的空数组
int[] resultArray = new int[getTreeCount(root)];
//数组当前位置
int index = 0;
//循环判断队列不为空
while (!q.isEmpty()) {
//取出队头元素
TreeNode tempNode = q.poll();
//将队头元素加入数组
resultArray[index++] = tempNode.val;
if (tempNode.left != null) {
q.add(tempNode.left);
}
if (tempNode.right != null) {
q.add(tempNode.right);
}
}
return resultArray;
}
//得到树的个数
private static int getTreeCount(TreeNode root) {
if (root == null) return 0;
return (getTreeCount(root.left) + getTreeCount(root.right) + 1);
}
}
需要注意的地方:
Queue的创建
剑指 Offer 32 - II. 从上到下打印二叉树 II
剑指 Offer 32 - II. 从上到下打印二叉树 II
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
提示:
节点总数 <= 1000
思路
该题也是需要层序遍历(必会)
然后,要确定二叉树的一层正好是二维数组的行(一维数组)
在之前做的题目当中,有一个求二叉树高度的题,也是同样的原理
数据结构与算法—树---翻转二叉树、二叉树的高度
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
//边界条件
if (root == null) return new ArrayList<List<Integer>> ();
//建立一个二维数组
List<List<Integer>> list = new ArrayList<List<Integer>> ();
//建立一个队列
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
//队列中元素个数
int queueCount = 1;
List<Integer> tempList = new ArrayList<Integer> ();
while (!q.isEmpty()) {
TreeNode tempNode = q.poll();
tempList.add(tempNode.val);
queueCount--;
if (tempNode.left != null) {
q.add(tempNode.left);
}
if (tempNode.right != null) {
q.add(tempNode.right);
}
if (queueCount == 0) {
queueCount = q.size();
List<Integer> subList = new ArrayList<Integer> ();
for (int i = 0; i < tempList.size(); i++) {
subList.add(tempList.get(i));
}
list.add(subList);
tempList.clear();
}
}
return list;
}
}
需要注意的地方
List的创建
ArrayList的操作不熟悉
剑指 Offer 32 - III. 从上到下打印二叉树 III
剑指 Offer 32 - III. 从上到下打印二叉树 III
请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[20,9],
[15,7]
]
提示:
节点总数 <= 1000
思路
跟第二道题一样,只需要加入一个布尔值,判断当前需要是从左到右,还是从右到左打印即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
//边界条件
if (root == null) return new ArrayList<List<Integer>> ();
//建立一个二维数组
List<List<Integer>> list = new ArrayList<List<Integer>> ();
//建立一个队列
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
//队列中元素个数
int queueCount = 1;
List<Integer> tempList = new ArrayList<Integer> ();
//是否是从左到右打印
boolean leftToRight = true;
while (!q.isEmpty()) {
TreeNode tempNode = q.poll();
tempList.add(tempNode.val);
queueCount--;
if (tempNode.left != null) {
q.add(tempNode.left);
}
if (tempNode.right != null) {
q.add(tempNode.right);
}
if (queueCount == 0) {
queueCount = q.size();
List<Integer> subList = new ArrayList<Integer> ();
if (leftToRight) {
for (int i = 0; i < tempList.size(); i++) {
subList.add(tempList.get(i));
}
}else{
for (int i = tempList.size() - 1; i >= 0; i--) {
subList.add(tempList.get(i));
}
}
leftToRight = !leftToRight;
list.add(subList);
tempList.clear();
}
}
return list;
}
}
本文介绍如何使用层序遍历的方式打印二叉树,包括从上到下、按层打印及之字形打印等不同场景,并提供详细的Java代码实现。

468

被折叠的 条评论
为什么被折叠?



