需要了解树的顺序存储

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

如果是满二叉树,底层用的是数组去放的,而数组放的时候 会有索引对应 当前父节点是索引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. Solon2 之 Groovy 语言开发后端接口项目

    今天再做个新的尝试,用 Solon 框架写个 Groovy 后端项目.借助 "Solon Initializr" 生成个项目模板,会比较方便. 1.生成项目模板 打开"S ...

  2. 听说你想用免费的FOFA?(第二弹)

    听说你想用免费的FOFA?(第二弹) 上回说到 听说你想用免费的FOFA? 第二弹 记得那是一个阳光正好的午后,我刚更新了导出文件类型,到了晚上就发现fofa hack下载不了了,看了一下最新的规则, ...

  3. 三天吃透Redis八股文

    Redis连环40问,绝对够全! Redis是什么? Redis(Remote Dictionary Server)是一个使用 C 语言编写的,高性能非关系型的键值对数据库.与传统数据库不同的是,Re ...

  4. Nvidia Tensor Core-WMMA API编程入门

    1 WMMA (Warp-level Matrix Multiply Accumulate) API 对于计算能力在7.0及以上的CUDA设备,可以使用CUDA C++ API调用Tensor Cor ...

  5. Linux网络管理入门

    根据自己的需要来设置Linux的一些属性 网络状态查看 在终端输入ifconfig可以查看网络状态 # ifconfig eth0: flags=4163<UP,BROADCAST,RUNNIN ...

  6. ArrayList实现原理和自动扩容

    ArrayList在Java集合中的位置, ArrayList原理: transient Object[] elementData; ArrayList通过数组来实现. 默认构造方法会构造一个容量为1 ...

  7. asyncio的基本使用框架,python高效处理数据,asyncio.gather(),asyncio. create_task(),asyncio.run(main())

    asyncio 是 Python 3.4 引入的标准库,是一个基于事件循环的异步 I/O 并发库.它提供了一种协作式的多任务处理方式,使得我们能够在一个线程中并发处理多个 I/O 操作.它通过将 I/ ...

  8. 当 SQL Server(mssql-jdbc) 遇上 BigDecimal → 精度丢失,真坑!

    开心一刻 中午和哥们一起喝茶 哥们说道:晚上喝酒去啊 我:不去,我女朋友过生日 哥们瞪大眼睛看着我:你有病吧,充气的过什么生日 我生气到:有特么生产日期的好吧 需求背景 系统对接了外部系统,调用外部系 ...

  9. 【问题解决】RabbitMQ启动出现epmd error for host xx.xx: nxdomain (non-existing domain)

    问题描述 [k8s]或[普通容器]或[Linux]部署的RabbitMQ启动时出现了 epmd error for host xx.xx: nxdomain (non-existing domain) ...

  10. 极速进化,光速转录,C++版本人工智能实时语音转文字(字幕/语音识别)Whisper.cpp实践

    业界良心OpenAI开源的Whisper模型是开源语音转文字领域的执牛耳者,白璧微瑕之处在于无法通过苹果M芯片优化转录效率,Whisper.cpp 则是 Whisper 模型的 C/C++ 移植版本, ...