【广度优先搜索】队列: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;
    }
}
相关推荐
小七在进步7 小时前
数据结构:堆排序
数据结构
可编程芯片开发18 小时前
基于MATLAB的PEF湍流风场生成器模拟与仿真
算法
罗西的思考19 小时前
【Agentic RL / 强化学习 / OPD】OpenClaw-RL 源码阅读笔记 --- (9)--- Reward Judging
人工智能·算法·机器学习
2401_8414956419 小时前
【数据结构】B+树
数据结构·数据库·c++·b+树·概念·结构·操作原理
Mr.HeBoYan20 小时前
一次持续三天才出现的丢包故障——深入解析 DPDK Memory Ordering、rte_ring 与 CPU Memory Barrier (下)
linux·网络·算法·架构·dpdk
wabs66621 小时前
关于图论【深度优先搜索的理论基础】
算法·深度优先·图论
皓月斯语21 小时前
【C++基础】三目运算符
开发语言·数据结构·c++
先吃饱再说21 小时前
LeetCode 101. 对称二叉树
算法
是Dream呀1 天前
人眼看不出的伪造,AI如何识别?实测合合信息跨模态鉴伪与去反光技术
算法
QN1幻化引擎1 天前
SFA 信号场注意力:用8KB参数换248x KV Cache压缩,边缘设备也能跑长序列
人工智能·算法·gitee·gitlab