力扣 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 ...
随机推荐
- LeeCode 栈与队列问题(二)
LeeCode 239: 滑动窗口最大值 题目描述 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右 ...
- 【Vue项目】尚品汇(六)ShopCar组件开发 购物车模块
4 购物车 4.1 购物车商品数量控制 Detail\index.vue <div class="cartWrap"> <div class="cont ...
- 生产事故-走近科学之消失的JWT
入职多年,面对生产环境,尽管都是小心翼翼,慎之又慎,还是难免捅出篓子.轻则满头大汗,面红耳赤.重则系统停摆,损失资金.每一个生产事故的背后,都是宝贵的经验和教训,都是项目成员的血泪史.为了更好地防范和 ...
- python工程师-day83
1.drf 的用户认证组件 (1)models from django.db import models# Create your models here.class User(models.Mode ...
- 笔记八:linux系统编程之IO
笔记:linux系统编程之IO 应用层 内核层 硬件层 应用层:数据结构 .java.android.C.C++,C#: l inux高级编程:涉及内核为应用层提供接口函数: 内核五大 ...
- 搭建SpringBoot项目依赖和配置快速篇
maven依赖及一些配置 这里主要是搭建项目常用到的maven依赖以及搭建项目会需要用到的一些配置文件,可能下面这些依赖还不是很全,但是应该会满足日常大部分的需求了 Spring Spring项目的依 ...
- 如何通过C#/VB.NET代码将PowerPoint转换为HTML
利用PowerPoint可以很方便的呈现多媒体信息,且信息形式多媒体化,表现力强.但难免在某些情况下我们会需要将PowerPoint转换为HTML格式.因为HTML文档能独立于各种操作系统平台(如Un ...
- Azure DevOps(三)Azure Pipeline 自动化将程序包上传到 Azure Bolb Storage
一,引言 结合前几篇文章,我们了解到 Azure Pipeline 完美的解决了持续集成,自动编译.同时也兼顾了 Sonarqube 作为代码扫描工具.接下来另外一个问题出现了,Azure DevOp ...
- 2022-12-09:上升的温度。以下的数据输出2和4,2015-01-02 的温度比前一天高(10 -> 25),2015-01-04 的温度比前一天高(20 -> 30),sql语句如何写? DR
2022-12-09:上升的温度.以下的数据输出2和4,2015-01-02 的温度比前一天高(10 -> 25),2015-01-04 的温度比前一天高(20 -> 30),sql语句如 ...
- 2022-12-07:删除重复的电子邮箱。删除重复数据后,id=3的数据被删除。请问sql语句如何写? DROP TABLE IF EXISTS `person`; CREATE TABLE `per
2022-12-07:删除重复的电子邮箱.删除重复数据后,id=3的数据被删除.请问sql语句如何写? DROP TABLE IF EXISTS `person`; CREATE TABLE `per ...