day09
144. 二叉树的前序遍历
题目
给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
示例 1:
输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
输入:root = [1,2]
输出:[1,2]
示例 5:
输入:root = [1,null,2]
输出:[1,2]
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
递归解法
/**
* https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
*
* @author xiexu
* @create 2022-03-08 11:29 AM
*/
public class _144_二叉树的前序遍历 {
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
preorder(root, list);
return list;
}
public void preorder(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
list.add(root.val);
preorder(root.left, list);
preorder(root.right, list);
}
}
迭代解法
思路
前序遍历是中左右,每次先处理的是中间节点,那么先将根节点放入栈中,然后将右孩子加入栈,再加入左孩子。
为什么要先加入 右孩子,再加入左孩子呢? 因为这样出栈的时候才是中左右的顺序。
代码实现
/**
* https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
*
* @author xiexu
* @create 2022-03-08 11:40 AM
*/
public class _144_二叉树的前序遍历_迭代法 {
// 前序遍历顺序:中-左-右,入栈顺序:中-右-左
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
list.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return list;
}
}
94. 二叉树的中序遍历
题目
给定一个二叉树的根节点 root
,返回它的 中序遍历。
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
输入:root = [1,2]
输出:[2,1]
示例 5:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IOrq1ky3-1646742872124)(day09%20f8681/Untitled%205.png)]
输入:root = [1,null,2]
输出:[1,2]
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
递归解法
/**
* https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
*
* @author xiexu
* @create 2022-03-08 11:37 AM
*/
public class _94_二叉树的中序遍历 {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
inorder(root, list);
return list;
}
public void inorder(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
inorder(root.left, list);
list.add(root.val);
inorder(root.right, list);
}
}
迭代解法
思路
代码实现
/**
* https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
*
* @author xiexu
* @create 2022-03-08 11:48 AM
*/
public class _94_二叉树的中序遍历_迭代法 {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
145. 二叉树的后序遍历
题目
给你一棵二叉树的根节点 root
,返回其节点值的 后序遍历 。
示例 1:
输入:root = [1,null,2,3]
输出:[3,2,1]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
递归解法
/**
* https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
*
* @author xiexu
* @create 2022-03-08 11:34 AM
*/
public class _145_二叉树的后序遍历 {
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
postorder(root, list);
return list;
}
public void postorder(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
postorder(root.left, list);
postorder(root.right, list);
list.add(root.val);
}
}
迭代解法
思路
先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图:
代码实现
/**
* https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
*
* @author xiexu
* @create 2022-03-08 11:54 AM
*/
public class _145_二叉树的后序遍历_迭代法 {
// 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
list.add(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
Collections.reverse(list);
return list;
}
}
102. 二叉树的层序遍历
题目
给你二叉树的根节点 root
,返回其节点值的 层序遍历。 (即逐层地,从左到右访问所有节点)。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000
思路
层序遍历一个二叉树。就是从左到右一层一层的去遍历二叉树。这种遍历的方式和我们之前讲过的都不太一样。
需要借用一个辅助数据结构即队列来实现,队列先进先出,符合一层一层遍历的逻辑,而用栈先进后出适合模拟深度优先遍历也就是递归的逻辑。
而这种层序遍历方式就是图论中的广度优先遍历,只不过我们应用在二叉树上。
使用队列实现二叉树广度优先遍历,动画如下:
代码实现
/**
* https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
*
* @author xiexu
* @create 2022-03-08 7:22 PM
*/
public class _102_二叉树的层序遍历 {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
// 从上到下遍历二叉树的每一层
while (!queue.isEmpty()) {
int n = queue.size();
ArrayList<Integer> list = new ArrayList<>();
// 从左到右遍历每一层的每个节点
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
list.add(node.val);
// 将下一层节点放入队列
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res.add(list);
}
return res;
}
}
107. 二叉树的层序遍历 II
题目
给你二叉树的根节点 root
,返回其节点值 自底向上的层序遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[15,7],[9,20],[3]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000
思路
相对于102.二叉树的层序遍历,就是最后把res数组反转一下就可以了。
代码实现
/**
* https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
*
* @author xiexu
* @create 2022-03-08 7:31 PM
*/
public class _107_二叉树的层序遍历_II {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int n = queue.size();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res.add(list);
}
// 反转二层集合的操作
List<List<Integer>> result = new ArrayList<>();
for (int i = res.size() - 1; i >= 0; i--) {
result.add(res.get(i));
}
return result;
}
}
199. 二叉树的右视图
题目
给定一个二叉树的 根节点 root
,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例 1:
输入: [1,2,3,null,5,null,4]
输出: [1,3,4]
示例 2:
输入: [1,null,3]
输出: [1,3]
示例 3:
输入: []
输出: []
提示:
二叉树的节点个数的范围是 [0,100]
-100 <= Node.val <= 100
思路
层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进res数组中,随后返回res就可以了。
代码实现
/**
* https://leetcode-cn.com/problems/binary-tree-right-side-view/
*
* @author xiexu
* @create 2022-03-08 7:47 PM
*/
public class _199_二叉树的右视图 {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int n = queue.size();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
if (i == n - 1) {
res.add(node.val);
}
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
return res;
}
}
637. 二叉树的层平均值
题目
给定一个非空二叉树的根节点 root
, 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5
以内的答案可以被接受。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[3.00000,14.50000,11.00000]
解释:第 0 层的平均值为 3,第 1 层的平均值为 14.5,第 2 层的平均值为 11 。
因此返回 [3, 14.5, 11] 。
示例 2:
输入:root = [3,9,20,15,7]
输出:[3.00000,14.50000,11.00000]
提示:
树中节点数量在 [1, 104] 范围内
-2^31 <= Node.val <= 2^31 - 1
思路
本题就是层序遍历的时候把每一层求个总和再取一个均值。
代码实现
/**
* https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/
*
* @author xiexu
* @create 2022-03-08 8:01 PM
*/
public class _637_二叉树的层平均值 {
public List<Double> averageOfLevels(TreeNode root) {
ArrayList<Double> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int n = queue.size();
double sum = 0; // 统计每一层的和
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
sum += node.val;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res.add(sum / n); // 将每一层的平均值放进结果集
}
return res;
}
}
429. N 叉树的层序遍历
题目
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。
树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
示例 1:
输入:root = [1,null,3,2,4,null,5,6]
输出:[[1],[3,2,4],[5,6]]
示例 2:
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
提示:
树的高度不会超过 1000
树的节点总数在 [0, 10^4] 之间
代码实现
/**
* https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
*
* @author xiexu
* @create 2022-03-08 8:13 PM
*/
public class _429_N叉树的层序遍历 {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
Queue<Node> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int n = queue.size();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
Node node = queue.poll();
list.add(node.val);
List<Node> children = node.children;
if (children == null || children.size() == 0) {
continue;
}
// 将节点孩子加入队列
for (Node child : children) {
if (child != null) {
queue.offer(child);
}
}
}
res.add(list);
}
return res;
}
}