层序遍历
层序遍历:即逐层地,从左到右依次访问所有节点,示例如下:
- 看见这种按序取元素,并且按序取每个元素的孩子,可以借助辅助数据结构来实现
- 即队列,队列先进先出,符合一层一层遍历的逻辑,而使用栈(先进后出)适合模拟深度优先遍历也就是递归逻辑
- 因此借助队列实现层序遍历十分合适
本文将通过以下例题验证层序遍历在一般场景的应用情况。
102 二叉树的层序遍历
题目
解题思路
- 此题借助队列实现,先将root节点加入队列中
- 然后以队列是否为空作为循环结束条件,若队列不为空则一直遍历
- 弹出root节点,并先后将左子节点和右子节点加入队列中,但问题是如何判断该层已经遍历结束了,以队列为空肯定是不可以的
- 用变量len记录遍历该层前,队列元素长度,即len为0时,表明该层已经遍历结束,其所有子节点均已加入队列中
- 每个点进出队列各一次,时间复杂度为O(n),队列中元素也不会超过n,空间复杂度为O(n)
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if(root == null) return result;
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
List<Integer> item = new ArrayList<>();
while(len > 0){
root = queue.poll();
if(root.left != null) queue.offer(root.left);
if(root.right != null) queue.offer(root.right);
if(root != null) item.add(root.val);
len--;
}
result.add(item);
}
return result;
}
107 二叉树层次遍历Ⅱ
题目
解题思路
- 解题思路与上题102二叉树的层次遍历一致
- 将102题答案进行倒置即可
- 每个点进出队列各一次,时间复杂度为O(n),队列中元素也不会超过n,空间复杂度为O(n)
public List<List<Integer>> levelOrderBottom(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> result = new ArrayList<>();
int len = 0;
if(root == null) return result;
queue.offer(root);
while(!queue.isEmpty()){
len = queue.size();
List<Integer> item = new ArrayList<>();
while(len > 0){
root = queue.poll();
if(root.left != null) queue.offer(root.left);
if(root.right != null) queue.offer(root.right);
item.add(root.val);
len--;
}
result.add(item);
}
Collections.reverse(result); // 列表反转
return result;
}
199 二叉树的右视图
题目
解题思路
- 按照题目理解,可以简单理解为,要求获取每层最右边元素
- 可以采用二叉树的层序遍历思路去解决,只需要将当层队列最后一个元素加入结果数组中即可。
- 每个点进出队列各一次,时间复杂度为O(n),队列中元素也不会超过n,空间复杂度为O(n)
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
int len = 0;
if(root == null) return result;
queue.offer(root);
while(!queue.isEmpty()){
len = queue.size();
while(len > 0){
root = queue.poll();
if(root.left != null) queue.offer(root.left);
if(root.right != null) queue.offer(root.right);
// 判断是否为当层最后一个元素
if(len == 1) result.add(root.val);
len--;
}
}
return result;
}
637 二叉树的层平均值
题目
解题思路
- 同理,此题可以采用二叉树层次遍历思路去解决
- 在循环获取当前队列值前,先记录当前队列元素长度为n,此时队列元素均为同一层元素
- 使用sum求和将n次弹出元素值求和,并除n以获取平均值,最后记录到结果数组中
- 每个点进出队列各一次,时间复杂度为O(n),队列中元素也不会超过n,空间复杂度为O(n)
public List<Double> averageOfLevels(TreeNode root) {
List<Double> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
int len = 0;
int len1 = 0; // 用于记录除数,也就是当前层具有多少元素
if(root == null) return result;
queue.offer(root);
while(!queue.isEmpty()){
len = queue.size();
len1 = len;
// 此处应使用double类型,而不是其包装类型Double
// 否则计算时拆箱时间过长,导致时间大幅上升
double sum = 0.0;
while(len > 0){
root = queue.poll();
if(root.left != null) queue.offer(root.left);
if(root.right != null) queue.offer(root.right);
sum += root.val;
len--;
}
result.add(sum/len1);
}
return result;
}
429 N叉树的层序遍历
题目
解题思路
- 观察上面定义节点方式,可以发现每个节点的子节点为一个列表
- 可以类比于二叉树的层序遍历,依次将每层节点加入队列中,只是在获取该节点的子节点时候,再次进行遍历加入对立中即可。
- 每个点进出队列各一次,时间复杂度为O(n),队列中元素也不会超过n,空间复杂度为O(n)
public List<List<Integer>> levelOrder(Node root) {
Queue<Node> queue = new LinkedList<>();
List<List<Integer>> result = new ArrayList<>();
int len = 0;
if(root == null) return result;
queue.add(root);
while(!queue.isEmpty()){
len = queue.size();
List<Integer> item = new ArrayList<>();
while(len > 0){
root = queue.poll();
if(root.children != null){
for(Node node : root.children) queue.offer(node);
}
item.add(root.val);
len--;
}
result.add(item);
}
return result;
}
117 填充每个节点的下一个右侧节点指针Ⅱ
题目
解题思路
- 此题还是可以依据二叉树的层序遍历实现
- 主要思路是,利用队列存储每一层元素节点,之后依次弹出该层队列元素并依次指定next指针
- 那么此题需要考虑俩个问题,1. 如何找到该层头节点 2. 何时结束该层
- 该层头节点为队列头节点
- 一般在进行队列弹出前,先记录队列长度size,当弹出队列元素数量等于size时,表明该层元素已经全部弹出
- 可以使用俩个临时节点(prev, last)记录前后俩个节点,一般情况下prev.next=last,但当prev为最左节点时,而last在循环开始时初始化为空,如果prev.next=last,那么prev指向null,这是错误的
- 此外,每次遍历过程中prev始终为last的上一个节点
- 每个点进出队列各一次,时间复杂度为O(n),队列中元素也不会超过n,空间复杂度为O(n)
public Node connect(Node root) {
Queue<Node> queue = new LinkedList<>();
if(root != null) queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
Node prev = null;
for(int i = 0; i < len; i++){
Node last = queue.poll();
if(last.left != null) queue.offer(last.left);
if(last.right != null) queue.offer(last.right);
if(i != 0) prev.next = last; // 判断是否为该层第一个节点,若为则跳过
prev = last;
}
}
return root;
}
111 二叉树的最小深度
题目
解题思路
- 此题依旧可以基于二叉树的层次遍历思路进行解题
- 在二叉树中共有倆层遍历,最外层遍历是遍历所有节点,第二层遍历是遍历同层节点
- 判断最小深度,则是离root节点最近叶子节点的深度
- 统计最小树深度可以在第二层遍历外进行统计,因为每一次while(!queue.isEmpty())都是新的一层
- 当第二层遍历节点中存在某节点为叶子节点,则返回此时树深
public int minDepth(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
int depth = 0;
queue.offer(root);
int len;
if(root == null) return 0;
while(!queue.isEmpty()){
len = queue.size();
depth++;
for(int i = 0; i < len; i++){
root = queue.poll();
if(root.left == null && root.right == null) return depth; // 为叶子节点,返回树深
if(root.left != null) queue.offer(root.left);
if(root.right != null) queue.offer(root.right);
}
}
return depth;
}