【广度优先搜索】队列: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;
    }
}
相关推荐
qq_416018722 小时前
移动平台C++开发指南
开发语言·c++·算法
王璐WL2 小时前
【C++】string的经典算法题
开发语言·c++·算法
闻缺陷则喜何志丹2 小时前
【动态规划】P8591 『JROI-8』颅脑损伤 2.0|普及+
c++·算法·动态规划·洛谷
阿贵---2 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
倾心琴心2 小时前
【agent辅助pcb routing coding学习】实践7 length matching 算法学习
学习·算法·agent·pcb·routing
y = xⁿ2 小时前
【LeetCodehot100】T114:二叉树展开为链表 T105:从前序与中序遍历构造二叉树
java·算法·链表
灰色小旋风2 小时前
力扣20有效的括号(C++)
c++·算法·leetcode·职场和发展
逆境不可逃2 小时前
LeetCode 热题 100 之 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表 142. 环形链表 II
算法·leetcode·链表
CoovallyAIHub2 小时前
AAAI 2026 | 华中科大联合清华等提出Anomagic:跨模态提示零样本异常生成+万级AnomVerse数据集(附代码)
深度学习·算法·计算机视觉