leetcode 662. Maximum Width of Binary Tree(二叉树的最大宽度)

flink新版本无bat启动文件的解决办法 flink 阅读详情

在这里插入图片描述

计算二叉树的最大宽度,宽度是指每层的最右边到最左边节点的距离。
从最左边的非null节点到最右边的非null节点(包含它们中间的null节点)。

思路:

1.BFS

与以往不同的是,这里不仅仅是统计一层的节点个数,还要算上节点中间的null(左右边界外的null不算)。
把null节点也转入queue会浪费空间,还要和边界外的null区分,比较麻烦,
因为是二叉树,所以给每个节点一个id, 左节点的id = root id * 2, 右节点id = root id * 2+ 1.
id可以就这么顺序排下去,如果担心id过大会溢出,
可以让每一层的id都从0开始(cur_id = id-left)。

    public int widthOfBinaryTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int maxWidth = 0;

        //用node.val保存id,这会修改掉节点值,题里不影响,实际情况要斟酌
        root.val = 0;
        queue.add(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            int left = queue.peek().val; //最左边节点的id
            int right = left;
            for(int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if(cur.left != null) {
                    //担心id过大,让每一层id都从0开始,其实不减left也可以,并不会超过int最大值
                    cur.left.val = (cur.val-left) * 2;
                    queue.add(cur.left);
                }
                if(cur.right != null) {
                    //担心id过大,让每一层id都从0开始,其实不减left也可以,并不会超过int最大值
                    cur.right.val = (cur.val-left) * 2 + 1;
                    queue.add(cur.right);
                }

                if(i == size - 1) right = cur.val;
            }
            maxWidth = Math.max(maxWidth, right-left+1);
        }
        return maxWidth;
    }

2.DFS

用DFS递归求每个节点的左右子树最大宽度,最大宽度是在 当前层的当前node到最左边node的距离,左子树的最大宽度,右子树的最大宽度,三者中取最大。
其中左子树宽度,右子树宽度也是递归,在 当前层的当前node到最左边node的距离,左子树的最大宽度,右子树的最大宽度,三者中取最大。
直到到达null节点,宽度返回0。

至于如何计算距离,可给每个node一个id,左子树的id就是root id✖️2,右子树id是root id✖️2+1,距离就是当前id减最左边id再加1
而如何判断最左边的id,可用一个list保存每层最左边的id。每层的第一个节点,也就是层数==list的size时加入节点id

    public int widthOfBinaryTree(TreeNode root) {
        List<Integer> ids = new ArrayList<>();
        return dfs(root, ids, 0, 0);
    }
    
    int dfs(TreeNode root, List<Integer> ids, int depth, int id) {
        if(root == null) return 0;
        if(depth == ids.size()) ids.add(id);
        
        return Math.max(id - ids.get(depth) + 1,  //当前层width
           Math.max(dfs(root.left, ids, depth+1, (id-ids.get(depth))*2), //左子树width
                       dfs(root.right, ids, depth+1, (id-ids.get(depth))*2+1)));
    }

参考资料

YOLO 系列专栏(二十九)YOLO26 × StarNet 轻量化涨点方案(CVPR 2024)轻量化、主干改进篇 | 星星卷积虽小,却蕴藏着巨大的高效潜力 StarNet 出自 CVPR 2024 轻量化主干网络工作,核心创新是,依靠逐元素乘法实现高阶特征融合,在几乎不增加参数量与 FLOPs 的前提下,大幅提升特征表达能力,天然适合等多任务统一建模。本文将 StarNet 完整替换 YOLO26 原有主干,保留 YOLO26 无 NMS 检测头与轻量化结构,实现,并提供。 阅读详情

相关推荐

开发基于GDL235KBQ6开发板的IAP升级程序(HEX协议+串口空闲中断 + DMA接收数据)

使用GDL235kB开发板,实现串口iap升级程序的bootloader代码编写,app为led灯闪烁,并完成通信c代码

zhaozijuncc的博客 656

LeetCode·每日一题·662.二叉树最大宽度·递归·迭代

递归·迭代

m0_64560763的博客 365

codesys【modbusRTU】主站

【代码】modbusRTU【codesys】

cfqq1989的博客 4408

leetcode--第662题--中等--《二叉树最大宽度》

刷题笔记

GuoShao_的博客 234

ESP32S3编译micropython固件

编译ESP32芯片的micropython固件

m0_73253860的博客 2560

2026年OPPO嵌入式岗位面试题带详细答案

本文总结了OPPO嵌入式岗位面试的核心考点,涵盖硬件驱动、系统启动、Linux内核开发等7大模块共34道典型题目。重点解析了SPI通信协议、中断处理、U-Boot启动流程、Linux驱动开发等关键技术,突出硬件调试与工程实践能力考核。题目设置贴合实际岗位需求,适合校招与社招驱动开发岗位应聘者系统复习,帮助掌握嵌入式开发的核心知识与面试要点。

Open 505

题目地址(662. 二叉树最大宽度)

给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与满二叉树(full binary tree)结构相同,但一些节点为空。 每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null节点也计入长度)之间的长度。...

y1040468929的博客 178

VxWorks系列 快速导航

Vxworks系列快速阅览导航

Mr.Sugarcane 1342

LeetCode 662 Maximum Width of Binary Tree 二叉树的最大宽度

LeetCode 662 Maximum Width of Binary Tree 二叉树的最大宽度 题目描述 Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tre

andyL_05的博客 471

微软商店报错“此产品当前不可用”?终极修复指南

微软商店报错“此产品当前不可用”?终极修复指南

安德洛夫 3880

python抓取美团外卖的数据(商家、评分、评论)

本文详细介绍了如何使用Selenium和抓取美团外卖的商家、评分和评论数据。我们从基础的爬虫原理入手,分析了美团外卖的网页结构,提出了应对反爬虫机制的策略,并给出了详细的代码实现。通过使用Selenium,我们能够绕过动态加载内容的限制,直接抓取网页中的商家信息和评论。结合多线程和代理池等技术,我们可以进一步提高爬虫效率,并避免IP被封禁。未来,随着爬虫技术的发展,我们可以进一步探索深度抓取、数据清洗和数据分析等高级应用,以实现更智能的网页数据抓取。

2201_76125261的博客 2720

Maximum Width of Binary Tree 二叉树最大宽度

[toc] Maximum Width of Binary Tree 二叉树最大宽度 二叉树的宽度: 每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null节点也计入长度)之间的长度。 输入:A = [3,4,5,1,2], B = [4,1] 输出:true输入: 1 / \ 3 2 / \ \ 5 3 9 输出: 4 解释: 最大值出现在树的第 3 层,宽度为

Cui's Blog 193

数仓建模—数据治理框架选型

全名主要亮点覆盖面最广:从数据治理、架构、质量、安全到元数据、主数据、数据仓库等等,几乎所有数据管理主题都能在DMBOK里找到相应章节。概念权威统一:帮助组织建立通用术语和知识基础,减少“部门之间说法不一致”的沟通成本。适用场景:如果你所在公司(或团队)正准备系统梳理数据管理的方方面面,DMBOK是一份非常全面的指导手册,尤其适合培养新人或为整个组织统一认知。需要注意:DMBOK偏“知识指引”,不直接提供成熟度模型或实施路线图。想要落地,还需要结合其他评估模型或项目管理方法。小案例举例:某。

2401_84052244的博客 4751

Leetcode 662.二叉树最大宽度(Maximum Width of Binary Tree)

Leetcode 662.二叉树最大宽度 1 题目描述(Leetcode题目链接)   给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与满二叉树(full binary tree)结构相同,但一些节点为空。 每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null节点也计入长度)之间的长度。 输入: 1 ...

qq_39378221的博客 268

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode : 662. Maximum Width of Binary Tree 最大二叉树宽度

试题: Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary...

心之所向 259

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode.662 —— 二叉树的最大宽度(Maximum Width of Binary Tree)

题目:https://leetcode.com/problems/maximum-width-of-binary-tree/ Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as afull bi.

Madoka_Homura的博客 512

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【LeetCode】662. Maximum Width of Binary Tree二叉树的最大宽度

662. Maximum Width of Binary Tree二叉树的最大宽度 Given a binary tree, write a function to get the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels. The width of one level is defined as the length between the end-n

ccystewart的博客 207

LeetCode 662. Maximum Width of Binary Tree 二叉树最大宽度 Java实现

题目:

zhangzhetaojj的博客 701

leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历。 层序遍历类似问题:leetcode 199. Binary Tree Right Side View | 199. 二叉树的右视图(Java) 如何使用“记录下一行最右节点”的方式,进行层序遍历:左神算法:二叉树的按层打印与ZigZag打印 一开始就想到了给每一个元素维护 position,但误以为 左孩子 pos-1,右孩子 pos+1

寒泉 4万+

[Swift]LeetCode662. 二叉树最大宽度 | Maximum Width of Binary Tree

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30908941的博客 124

LeetCode_Queue_662. Maximum Width of Binary Tree 二叉树最大宽度【队列,迭代】【中等】

目录 一,题目描述 英文描述 中文描述 示例与说明 二,解题思路 三,AC代码 C++ Java 四,解题过程 第一博 一,题目描述 英文描述 Given the root of a binary tree, return the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels. The width of one level i

&再见萤火虫&的博客 270

leetcode 662. Maximum Width of Binary Tree -- 二叉树最大宽度

二叉树宽度,广度优势搜索,bfs,二叉树层序遍历,队列同步

weixin_43887808的博客 229

leetcode 662. Maximum Width of Binary Tree 二叉树最大宽度-耗时100%

该题解采用深度优先搜索(DFS)策略计算二叉树的最大宽度。关键思路是为每个节点按照完全二叉树规则编号:根节点为0,左子节点为2index+1,右子节点为2index+2。使用哈希表记录每层的最小和最大编号,最终宽度即为同一层中最大编号与最小编号之差加1。算法时间复杂度O(n),空间复杂度O(n),其中n为节点数,通过遍历一次树并比较每层编号即可得到最大宽度。

邹九的个人博客 199

[LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as afull binary ...

weixin_30270889的博客 154

leetcode 662. Maximum Width of Binary Tree 二叉树最大宽度 + 深度优先遍历DFS

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tre...

JackZhangNJU的专栏 1381

LeetCode Mock Review 2021-4-11

LeetCode Mock Review 2021-04-11 118. Pascal’s Triangle 118. Pascal’s Triangle 一道数学题。第i层的第k个数字等于第i-1层的第k-1个数字和第k个数字的和。使用一个List保存上一层的数字,先添加首位的1然后填充后续数字,最后再添加末尾的1。 class Solution { public List<List<Integer>> generate(int numRows) { Lis

qq_40818336的博客 206
上一篇: leetcode 654. Maximum Binary Tree(最大二叉树)
下一篇: leetcode 669. Trim a Binary Search Tree(修剪二叉搜索树)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址