力扣 662 https://leetcode.cn/problems/maximum-width-of-binary-tree/
需要了解树的顺序存储
如果是普通的二叉树 ,底层是用链表去连接的
如果是满二叉树,底层用的是数组去放的,而数组放的时候 会有索引对应 当前父节点是索引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/的更多相关文章
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 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 ...
- [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 ...
- [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 ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- LeetCode之104. Maximum Depth of Binary Tree
-------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...
- 662. Maximum Width of Binary Tree
https://leetcode.com/problems/maximum-width-of-binary-tree/description/ /** * Definition for a binar ...
- 【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 ...
- 【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 ...
随机推荐
- DG:模拟failover故障与恢复
问题描述:情形是当主库真正出现异常之后,才会执行的操作,那么我们执行过failover 之后,如何在重新构建DG,这里我们利用flashback database来重构.模拟前主库要开启闪回区,否则要 ...
- el-menu使用递归组件实现多级菜单组件
1. 效果: 2. 实现: 创建外层菜单AsideMenu.vue组件和子菜单项AsideSubMenu.vue组件,在AsideSubMenu中进行递归操作. AsideMenu.vue文件内容如下 ...
- 即时通讯系统为什么选择GaussDB(for Redis)?
摘要:如果你需要一款稳定可靠的高性能企业级KV数据库,不妨试试GaussDB(for Redis). 每当网络上爆出热点新闻,混迹于各个社交媒体的小伙伴们全都开启了讨论模式.一条消息的产生是如何在群聊 ...
- Java线程中断机制
在阅读AQS源码以及juc包很多基于AQS的应用源码过程中,会遇到很多interrupted相关的方法,这里复习一下java线程中断相关. 要点:使用interrupt()中断一个线程,该方法只是标记 ...
- vue-cli3构建和发布 实现分环境打包步骤(给不同的环境配置相对应的打包命令)
https://panjiachen.github.io/vue-element-admin-site/zh/guide/essentials/deploy.html#%E6%9E%84%E5%BB% ...
- YOLO2论文中文版
文章目录 YOLO9000中文版 摘要 1. 引言 2. 更好 3. 更快 4. 更强 5. 结论 参考文献 YOLO9000中文版 摘要 我们引入了一个先进的实时目标检测系统YOLO9000,可以检 ...
- 任务系统之Jenkins子任务
今天下班即开启五一假期,早上临时定了行程去山东日照,原本计划下班就出发,但下班看了看导航,这一路红得发黑,于是决定还是晚点再走,现在有时间了,写篇简单的技术文章来提升下Blog逐渐降低的技术内容含量吧 ...
- 2022-10-07:给定员工的 schedule 列表,表示每个员工的工作时间。 每个员工都有一个非重叠的时间段 Intervals 列表,这些时间段已经排好序。 返回表示 所有 员工的 共同,正
2022-10-07:给定员工的 schedule 列表,表示每个员工的工作时间. 每个员工都有一个非重叠的时间段 Intervals 列表,这些时间段已经排好序. 返回表示 所有 员工的 共同,正数 ...
- windows server 2012 2019启动 开机自动启动 项设置
1.第一种方法:打开运行功能,运行shell:startup,打开管理员用户启动项目录.将想要设置成开机自启的程序快捷方式添加到其中即可,或者删除其中快捷方式即可取消开机自启.2.第二种方法:打开系统 ...
- Python基础 - 注释
单行注释 Python中使用#表示单行注释.一般在#后面添加一个空格,再添加注释内容 1 # 这是单行注释 多行注释 Python中使用三个单引号或三个双引号表示多行注释. 1 ''' 2 这是使 ...