当前位置: 首页 > article >正文

二叉树——429,515,116

今天继续做关于二叉树层序遍历的相关题目,一共有三道题,思路都借鉴于最基础的二叉树的层序遍历。

LeetCode429.N叉树的层序遍历

N叉树的层序遍历
这道题不再是二叉树了,变成了N叉树,也就是该树每一个节点的子节点数量不确定,可能为2,可能为1,也可能为3等等。要求也是需要从左到右层序遍历,和二叉树的层序遍历类似,需要改动的地方有,每一个节点出队时,其叶子节点全部存于一个列表中,将这个列表中的全部元素入队即可,不再是将二叉树仅有的两个子节点:左子节点,右子节点入队。

	public static List<List<Integer>> levelOrder(Node root){
        List<List<Integer>> list=new ArrayList<>();
        Queue<Node> queue=new LinkedList<>();
        if (root==null){
            return list;
        }else {
            queue.offer(root);
        }
        Node node=root;
        while (!queue.isEmpty()){
            int size=queue.size();
            List<Integer> lst = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                node=queue.poll();
                if (node.children!=null) {
                    for (int j = 0; j < node.children.size(); j++) {
                        queue.offer(node.children.get(j));
                    }
                }
                lst.add(node.val);
            }
            list.add(lst);
        }
        return list;
    }

LeetCode515.在每个树行中找最大值

在每个树行中找最大值
这道题先层序遍历,可以将每一层的所有元素存入数组,然后比较数组中的所有元素,选出最大值,即为二叉树该层的最大值,如此循环,将二叉树的所有层都遍历完成。

	public static int researchMax(List<Integer> list){
        int max=list.get(0);
        for (int i = 0; i < list.size(); i++) {
            if (max<list.get(i)){
                max=list.get(i);
            }
        }
        return max;
    }
    
    public List<Integer> largestValues(TreeNode root){
        List<Integer> list=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        if (root==null){
            return list;
        }else {
            queue.offer(root);
        }
        TreeNode node;
        while (!queue.isEmpty()){
            List<Integer> lst = new ArrayList<>();
            int size= queue.size();
            for (int i = 0; i < size; i++) {
                node=queue.poll();
                if (node.left!=null){
                    queue.offer(node.left);
                }
                if (node.right!=null){
                    queue.offer(node.right);
                }
                lst.add(node.val);
            }
            list.add(researchMax(lst));
        }
        return list;
    }

LeetCode116.填充每个节点的下一个右侧节点指针

填充每个节点的下一个右侧节点指针
层序遍历,将每一个出队后的节点的next指针指向这时队列的peak。这里一定需要一个计数器,每次进入循环时,记录当前的队列长度,也就是当前树行的节点个数,如果遍历到最后一个节点时,后面没有节点了,这时就需要将next指针指向null值。

	public static Node connect(Node root){
        Queue<Node> queue=new LinkedList<>();
        if (root==null){
            return null;
        }else {
            queue.offer(root);
        }
        Node node;
        while (!queue.isEmpty()){
            int size= queue.size();
            for (int i = 0; i < size; i++) {
                node=queue.poll();
                if (i==size-1){
                    node.next=null;
                }else {
                    node.next=queue.peek();
                }
                if (node.left!=null){
                    queue.offer(node.left);
                }
                if (node.right!=null){
                    queue.offer(node.right);
                }
            }
        }
        return root;
    }

http://www.kler.cn/a/529441.html

相关文章:

  • conda配置channel
  • LabVIEW无线齿轮监测系统
  • 被裁与人生的意义--春节随想
  • 17.2 图形绘制7
  • 软件工程经济学-日常作业+大作业
  • 计算机网络一点事(21)
  • 031.关于后续更新和指纹浏览器成品
  • HTB:Alert[WriteUP]
  • 实现C语言的原子操作
  • 【机器学习】自定义数据集,使用scikit-learn 中K均值包 进行聚类
  • 第12章:基于TransUnet和SwinUnet网络实现的医学图像语义分割:腹部13器官分割(网页推理)
  • 成绩案例demo
  • 【FreeRTOS 教程 七】互斥锁与递归互斥锁
  • Java 中的 function 接口像一件艺术品
  • BUUCTF_[羊城杯2020]easyphp(构造特殊文件名,字符串拼接绕过/正则表达式/代码审计)
  • 【自开发工具介绍】SQLSERVER的ImpDp和ExpDp工具02
  • Swoole如何实现多进程
  • kamailio-ASYNC模块详解【以下内容来源于官网,该文章仅作为翻译】
  • Python淘宝电脑销售数据爬虫可视化分析大屏全屏系统 开题报告
  • 电信传输基本理论/5G网络层次架构——超三万字详解:适用期末考试/考研/工作
  • Redis背景介绍
  • Node.js 和 npm 安装教程
  • 99.24 金融难点通俗解释:MLF(中期借贷便利)vs LPR(贷款市场报价利率)
  • 软件工程概论试题四
  • 【Shell编程 / 8】脚本优化与高级功能:提高效率与自动化管理
  • 在 Windows 上安装 DeepSeek 的完整指南