【广度优先搜索】队列: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;
    }
}
相关推荐
听取WA声一片(无恶意)16 分钟前
CSP-J/S 初赛图论完全讲义
算法·csp-s初赛·csp-j初赛
Brilliantwxx28 分钟前
【算法从零到千】【51-54】逆序对(分治+递归算法)
数据结构·算法·排序算法
小星星闪亮登场37 分钟前
2026河南萌新联赛第四场--南阳理工学院
数据结构·算法·贪心算法·动态规划·哈希算法·广度优先
土司大王39 分钟前
LeetCode hot100——两数之和
数据结构·算法·leetcode
luj_17681 小时前
大航海时代:沉浸式财商实战沙盒
c语言·开发语言·网络·经验分享·算法
Navigator_Z1 小时前
LeetCode //C - 1200. Minimum Absolute Difference
c语言·算法·leetcode
wabs6662 小时前
关于字符串【力扣344.反转字符串的思考】
数据结构·算法·leetcode
LuminousCPP2 小时前
数据结构基础篇(二):顺序表与链表全方位对比|从内存布局到 CPU 缓存理解底层差异
c语言·数据结构·经验分享·链表·缓存
有点。2 小时前
C++二叉树(一)
开发语言·数据结构·c++
民乐团扒谱机2 小时前
【微实验】谐波乘积谱(HPS)算法深度解析:原理、数学与代码实现
开发语言·人工智能·python·算法·语音识别·音乐