需要了解树的顺序存储

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

如果是满二叉树,底层用的是数组去放的,而数组放的时候 会有索引对应 当前父节点是索引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. must be reducible node 错误

    "must be reducible node"错误通常是由于使用了无法转换为表达式树的代码或表达式. 场景再现:在项目中使用GroupBy的时候,对字段进行了类型转换,接下来正常 ...

  2. 从内核源码看 slab 内存池的创建初始化流程

    在上篇文章 <细节拉满,80 张图带你一步一步推演 slab 内存池的设计与实现 >中,笔者从 slab cache 的总体架构演进角度以及 slab cache 的运行原理角度为大家勾勒 ...

  3. bash shell 无法使用 perl 正则

    哈喽大家好,我是咸鱼.今天跟大家分享一个关于正则表达式的案例,希望能够对你有所帮助 案例现象 前几天有一个小伙伴在群里求助,说他这个 shell 脚本有问题,让大家帮忙看看   可以看到,这个脚本首先 ...

  4. php 正则去掉<p>&nbsp;</p> 空格 &nbsp;

    $str=' <p> </p><p> </p><p> </p><p> </p><p>< ...

  5. 理解Linux系统: 进程

    Linux内核版本: 2.6.11.12 编写代码: 创建进程 创建进程使用fork系统调用,官方文档对于fork的描述: fork() creates a new process by duplic ...

  6. 高阶组件——withRouter的原理和用法

    作用: 高阶组件中的withRouter, 作用是将一个组件包裹进Route里面, 然后react-router的三个对象history, location, match就会被放进这个组件的props ...

  7. 带大小写忽略的Replace

    #region 以下函数用于忽略大小写替换操作 public static string Replace(string Expression, string Find, string Replacem ...

  8. 2020-10-27:go中select的执行流程是什么?

    福哥答案2020-10-27: ***[2020-10-27:go中select的执行流程是什么?](https://bbs.csdn.net/topics/398044569)

  9. 2022-01-17:单词规律 II。 给你一种规律 pattern 和一个字符串 str,请你判断 str 是否遵循其相同的规律。 这里我们指的是 完全遵循,例如 pattern 里的每个字母和字符

    2022-01-17:单词规律 II. 给你一种规律 pattern 和一个字符串 str,请你判断 str 是否遵循其相同的规律. 这里我们指的是 完全遵循,例如 pattern 里的每个字母和字符 ...

  10. TokenObtainPairView

    TokenObtainPairView是由Django REST framework的SimpleJWT库提供的视图.它用于生成JSON Web Token(JWT)