力扣 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 ...
随机推荐
- day17:内置方法&math模块&random模块&pickle模块
内置方法 1.round:四舍五入 正常遵守四舍五入,但在n.5结构中,n为偶数则舍去,n为奇数则进一. res1 = round(4.51) # 5 res2 = round(4.5) # 4 re ...
- RFS[3]: No standby redo logfiles available for thread 1
问题描述:备库恢复DG之后,mrp进程一直是wait_for_log,主库创建数据没有正常同步,只有在切换归档的时候备库才能同步主库数据 查看主库日志,主库RFS进程提示没有可用的standby re ...
- 【vue3-element-admin】Husky + Lint-staged + Commitlint + Commitizen + cz-git 配置 Git 提交规范
前言 本文介绍 vue3-element-admin 如何通过 Husky + Lint-staged + Commitlint + Commitizen + cz-git 来配置 Git 提交代码规 ...
- 计算机常用的快捷键以及常用的Dos命令
计算机的常用快捷键有哪些? 今天我重温了Java基础的课程,计算机的快捷键大家肯定不陌生. 计算机的常用快捷键 ctrl+c 复制 ctrl+v 粘贴 ctrl+s 保存 ctrl+x 剪切 ct ...
- 《简化iOS APP上架流程,App Uploader助你搞定!》
转载;http://kxdang.com/topic/appuploader/questions.html Appuploader 常见错误及解决方法 问题解决秘籍 遇到问题,第一个请登录苹果开发 ...
- Node工程的依赖包管理方式
作者:京东零售 陈震 在前端工程化中,JavaScript 依赖包管理是非常重要的一环.依赖包通常是项目所依赖的第三方库.工具和框架等资源,它们能够帮助我们减少重复开发.提高效率并且确保项目可以正确的 ...
- Sql Server 数据库事务与锁,同一事务更新又查询锁?期望大家来解惑
我有一个People表,有三行数据: 如果我们没详细了解数据库事务执行加锁的过程中,会不会有这样一个疑问:如下的这段 SQL 开启了事务,并且在事务中进行了更新和查询操作. BEGIN TRAN up ...
- [Pytorch框架] 1.2、Pytorch环境搭建
文章目录 1.2 Pytorch环境搭建 1.2.1 安装Pytorch 1.2.2 配置 Jupyter Notebook 1.2.3 测试 1.2.4 问题解决 问题1:启动python提示编码错 ...
- Protobuf编码规则
支持类型 该表显示了在 .proto 文件中指定的类型,以及自动生成的类中的相应类型: .proto Type Notes C++ Type Java/Kotlin Type[1] Java/Kotl ...
- 【python爬虫】对站长网址中免费简历模板进行爬取
本篇仅在于交流学习 解析页面 可以采用xpath进行页面连接提取 进入页面 通过进入的页面可以得到下载地址 步骤: 提取表页面模板链接-->进入连接-->提取页面内下载地址连接--> ...