【广度优先搜索】队列:N叉树的层序遍历,二叉树的锯齿形层序遍历,二叉树的最大宽度,在每个树行中找最大值

文章目录

1. N叉树的层序遍历(LC429)

N叉树的层序遍历

题目描述

解题思路

  • 队列里存的是当前层的所有节点
  • 每次处理一层:先统计当前队列的大小,确定这一层有多少节点
  • 逐个取出节点,把值加入当前层的临时列表;同时把该节点的所有子节点依次入队,为下一层遍历做准备
  • 一层处理完后,把临时列表加入结果集
  • 重复直到队列为空

代码实现

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        if(root == null)
            return ret;

        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int sz = queue.size();
            //统计本层节点
            List<Integer> tmp = new ArrayList<>();
            for(int i = 0;i<sz;i++){
                Node top = queue.poll();
                tmp.add(top.val);

                //子节点入队
                for(Node child : top.children)
                    if(child != null)
                        queue.offer(child);
            }
            ret.add(tmp);
        }
        return ret;
    }
}

2. 二叉树的锯齿形层序遍历(LC103)

二叉树的锯齿形层序遍历

题目描述

解题思路

在层序遍历的基础上,增加一个标志位,标记当前行是顺序还是逆序。

代码实现

java 复制代码
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root==null)
            return ret;

        boolean flag = false;
        queue.offer(root);
        while(!queue.isEmpty()){
            int sz = queue.size();
            List<Integer> tmp = new ArrayList<>();
            for(int i = 0;i<sz;i++){
                TreeNode top = queue.poll();
                tmp.add(top.val);

                //子节点入队
                if(top.left!=null)
                    queue.offer(top.left);
                if(top.right!=null)
                    queue.offer(top.right);
            }
            if(flag)
                Collections.reverse(tmp);
            flag = !flag;
            ret.add(tmp);
        }
        return ret;
    }
}

3.二叉树的最大宽度(LC662)

二叉树的最大宽度

题目描述

解题思路

  • 参考堆的底层数组,把树当成完全二叉树,利用数组下标记录节点的序号。
  • 定义hashmap 保存TreeNode和对应下标,层序遍历时,计算每层的宽度仅需让尾下标-头下标+1
  • 从0开始计数,对于i节点
    • 左孩子:2i+1
    • 右孩子:2i+2

注意:

  1. 对于最有一行下标数值可能达到21500,这个数据int会溢出,但是求得到的差是正确的.
  2. 数组实现的列表头删的时间复杂度很高,所以直接新建一个临时列表添加元素,再覆盖原来的队列。

代码实现

java 复制代码
class Solution {
    public int widthOfBinaryTree(TreeNode root) {
        List<Pair<TreeNode,Integer>> q= new ArrayList<>();
        q.add(new Pair<TreeNode,Integer>(root,1));
        int ret = 0;
        while(!q.isEmpty()){
            //更新宽度
            Pair<TreeNode,Integer> head = q.get(0);
            Pair<TreeNode,Integer> last = q.get(q.size()-1);
            ret = Math.max(ret,(last.getValue()  - head.getValue() + 1));

            //下一层进队
            List<Pair<TreeNode,Integer>> tmp = new ArrayList<>();
            for(Pair<TreeNode,Integer> t : q){
                TreeNode node = t.getKey();
                int index = t.getValue();
                if(node.left != null)
                    tmp.add(new Pair<TreeNode,Integer> (node.left,index*2 +1));
                if(node.right != null)
                    tmp.add(new Pair<TreeNode,Integer> (node.right,index*2 +2));
            }
            q = tmp;
        }
        return ret;
    }
}

4.在每个树行中找最大值(LC515)

在每个树行中找最大值

题目描述

解题思路

层序遍历过程中,统计每一层的最大值

代码实现

java 复制代码
class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null)
            return ret;

        queue.offer(root);
        while(!queue.isEmpty()){
            int max = Integer.MIN_VALUE;
            int sz = queue.size();
            for(int i =0;i<sz;i++){
                TreeNode top = queue.poll();
                max = Math.max(max,top.val);

                if(top.left != null)
                    queue.offer(top.left);
                if(top.right != null)
                    queue.offer(top.right);
            }
            ret.add(max);
        }
        return ret;
    }
}
相关推荐
2401_8920709811 分钟前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
minji...1 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
语戚2 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
skywalker_112 小时前
力扣hot100-7(接雨水),8(无重复字符的最长子串)
算法·leetcode·职场和发展
bIo7lyA8v3 小时前
算法稳定性分析中的输入扰动建模的技术9
算法
CoderCodingNo3 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
sinat_286945193 小时前
AI Coding 时代的 TDD:从理念到工程落地
人工智能·深度学习·算法·tdd
炽烈小老头3 小时前
【 每天学习一点算法 2026/04/12】x 的平方根
学习·算法
ASKED_20193 小时前
从排序到生成:腾讯广告算法大赛 2025 baseline解读
人工智能·算法