【广度优先搜索】队列: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;
    }
}
相关推荐
inrichtt4 小时前
第八课时:SPI总线揭秘:四线全双工通信的奥
c语言·数据结构·stm32·单片机·嵌入式硬件
HPtiger5 小时前
数据结构——选择排序
数据结构·算法·排序算法
康实训5 小时前
老年评估仿真实训平台架构设计与老年人能力评估实训室教学落地实践
大数据·人工智能·算法·实训室·实训室建设
DongQiShanRen5 小时前
六根硬件映射(四):归属澄清、两处不一致与卷十一总表
开发语言·人工智能·算法·rust·哈希算法·集成学习
CCblandy5 小时前
雪花 ID 四个边界实测:时钟回拨重号 500 个,前端 JSON.parse 直接丢精度
后端·python·算法·性能优化·架构
丑梨数据6 小时前
信创环境里跑文档工具:格式兼容与本地合规的几个坑
数据结构·人工智能·算法·决策树·排序算法
烂蜻蜓6 小时前
Pandas入门教程(四):Series数据结构——创建、索引与常用方法
数据结构·pandas
erzong2317 小时前
Linux操作系统:线程概念与控制
linux·算法
Nil2088 小时前
leetcode 5最长回文子串
算法·leetcode·职场和发展
xxwl5858 小时前
区域赛分类题解
c++·笔记·学习·算法