需要了解树的顺序存储

如果是普通的二叉树 ,底层是用链表去连接的

如果是满二叉树,底层用的是数组去放的,而数组放的时候 会有索引对应 当前父节点是索引i,下一个左右节点就是2i,2i+1

利用满二叉树的索引特征

所以需要对每个节点进行一个索引赋值,赋值在队列中,队列用数组表示

核心代码如下

public int widthOfBinaryTreeBetter(TreeNode root) {
Queue<Pair<TreeNode,Integer>> queue = new LinkedList<>(); queue.add(new Pair<>(root,1));
int res=0;
while (!queue.isEmpty()){ int size = queue.size();
int left=-1;
int right=-1;
for (int i = 0; i < size; i++) {
Pair<TreeNode, Integer> poll = queue.poll();
Integer value = poll.getValue();
if (left==-1){
left=value;
}
right=value; TreeNode temp = poll.getKey();
if (temp.left!=null){
queue.add(new Pair<>(temp.left,value*2));
}
if (temp.right!=null){
queue.add(new Pair<>(temp.right,value*2+1));
}
res=Math.max(right-left+1,res);
} }
return res;
}

力扣 662 https://leetcode.cn/problems/maximum-width-of-binary-tree/的更多相关文章

  1. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  2. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  3. LC 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 tre ...

  4. [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 tre ...

  5. [LeetCode] 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 tre ...

  6. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  7. LeetCode之104. Maximum Depth of Binary Tree

    -------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...

  8. 662. Maximum Width of Binary Tree

    https://leetcode.com/problems/maximum-width-of-binary-tree/description/ /** * Definition for a binar ...

  9. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  10. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

随机推荐

  1. IIS 部署.NET CORE 项目 出现 HTTP 错误 500.19 - Internal Server Error

    当出现这个错误时是因为服务器上没有.NET CORE对应的SDK以及运行时文件,我的.NET CORE版本是2.2,下载的就是2.2对应的文件. 附上.NET CORE2.2版本的下载链接 下载 .N ...

  2. Java SpringBoot 7z 压缩、解压

    Java SpringBoot 7z 压缩.解压 cmd 7z 文件压缩 7z压缩测试 添加依赖 <dependency> <groupId>org.apache.common ...

  3. 从 HTTP 到 gRPC:APISIX 中 etcd 操作的迁移之路

    罗泽轩,API7.ai 技术专家/技术工程师,Apache APISIX PMC 成员. 原文链接 Apache APISIX 现有基于 HTTP 的 etcd 操作的局限性 etcd 在 2.x 版 ...

  4. 记一次 Windows10 内存压缩模块 崩溃分析

    一:背景 1. 讲故事 在给各位朋友免费分析 .NET程序 各种故障的同时,往往也会收到各种其他类型的dump,比如:Windows 崩溃,C++ 崩溃,Mono 崩溃,真的是啥都有,由于基础知识的相 ...

  5. C# 组合键判断

    e.KeyboardDevice.Modifiers 同时按下了Ctrl + H键(H要最后按,因为判断了此次事件的e.Key)修饰键只能按下Ctrl,如果还同时按下了其他修饰键,则不会进入 1 pr ...

  6. css知识点简记

    1.改变position: fixed; 定位基准元素的方式,父级以上元素的: ① tranform属性值不为none的元素 ② perspective值不为none的元素 ③ will-change ...

  7. Win10系统Anaconda下tensorflow的GPU环境搭建

    我的环境:Win10 + Anaconda + tensorflow-gpu1.14 + CUDA10.0 + cuDNN7.6 + python3.6 注意:tensorflow版本.CUDA版本. ...

  8. express服务器框架

    Express 为了提高开发效率,我们在开发过程中,都会尽量使用别人已经开发好的第三方模块,而我们想要快速实现服务器端的开发,推荐一个当下比较流行的框架:Express Express 作为开发框架, ...

  9. 2021-03-12:go中,如何确定有没有内存泄露,系统里怎么去监控整体的运行情况,日志是怎么处理的?

    2021-03-12:go中,如何确定有没有内存泄露,系统里怎么去监控整体的运行情况,日志是怎么处理的? 福哥答案2021-03-12: runtime/pprof:采集程序(非 Server)的运行 ...

  10. json在线效验检测工具

    json在线效验检测工具:https://www.sojson.com/ 解析结果: { 'os_type': 'Windows', 'os_release': '10 64bit 10.0.1904 ...