[LeetCode]662. Maximum Width of Binary Tree判断树的宽度
public int widthOfBinaryTree(TreeNode root) {
/*
层序遍历+记录完全二叉树的坐标,左孩子2*i,右孩子2*i+1
而且要有两个变量,一个记录本层节点数,一个记录下层节点数
层序遍历用队列实现
还要有一个队列记录本层的下标
*/
//层序遍历记录节点
Queue<TreeNode> tree = new LinkedList<>();
//记录每个节点的位置,用来判断此节点的孩子的坐标
Queue<Integer> index = new LinkedList<>();
//当前层数量和下一层数量
int cur = 1;
int next = 0;
//本层开始坐标和结束坐标
int sta = 1;
int end = 1;
//记录结果
int res = 0;
tree.offer(root);
index.offer(1);
while (!tree.isEmpty())
{
//当前节点和坐标
TreeNode curTree = tree.poll();
end = index.poll();
//添加左子树和坐标
if (curTree.left!=null)
{
tree.offer(curTree.left);
next++;
index.offer(end*2);
}
//添加右子树和坐标
if (curTree.right!=null)
{
tree.offer(curTree.right);
next++;
index.offer(end*2+1);
}
//本层待遍历节点数-1
cur--;
//本层遍历完毕,更新结果和下一层数据
if (cur==0)
{
res = Math.max(res,end-sta+1);
cur = next;
next = 0;
sta = index.isEmpty()?1:index.peek();
}
}
return res;
}
[LeetCode]662. Maximum Width of Binary Tree判断树的宽度的更多相关文章
- [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 ...
- 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 ...
- 【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 ...
- [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 ...
- 662. Maximum Width of Binary Tree
https://leetcode.com/problems/maximum-width-of-binary-tree/description/ /** * Definition for a binar ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- D. Numbers on Tree(构造)【CF 1287】
传送门 思路: 我们需要抓住唯一的重要信息点"ci",我的做法也是在猜想和尝试中得出的,之后再验证算法的正确性. 我们在构造中发现,如果树上出现了相同的数字,则会让树的构造变得不清 ...
- PyQt(Python+Qt)学习随笔:QToolBox工具箱的currentItemName和tabSpacing属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在Designer中,toolBox的属性中有currentItemName和tabSpacing属 ...
- 第8.29节 使用MethodType将Python __setattr__定义的实例方法与实例绑定
一. 引言 在<第7.14节Python类中的实例方法解析>介绍了使用"实例对象名.方法名 = MethodType(函数, 对象)"将动态定义的方法与实例进行绑定 在 ...
- Python中使用“模块名.__all__”查看模块建议导出的属性
在<第10.5节 使用__all__定义Python模块导入白名单>中,老猿介绍了在自定义模块中使用定义__all__属性来提供模块对外可见的白名单,使用该机制除了可以定义访问的白名单外, ...
- PyQt(Python+Qt)学习随笔:QListView的flow属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的flow属性用于控制视图中的数据排列方向,其类型为枚举类型QListView.F ...
- 题解-TJOI2015 弦论
TJOI2015 弦论 字符串 \(s\) 和 \(t\) 和 \(k\).如果 \(t=0\),不同位置的相同子串算 \(1\) 个:如果 \(t=1\),不同位置的相同子串算多个.求 \(k\) ...
- Java集合源码分析(四)——Vector
简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. 和ArrayList不同,Ve ...
- Java并发编程的艺术(三)——synchronized
什么是synchronized synchronized可以保证某个代码块或者方法被一个线程占有,保证了一个线程的可先性.java 1.6之前是重量级锁,在1.6进行了各种优化,就不那么重了,并引入了 ...
- 七、git学习之——使用GitHub、自定义Git、
原文来自 一.使用GitHub 我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人 ...
- springboot配置ssl证书
springboot默认使用的是tomcat: 1.先到阿里云上注册一个证书,绑定域名:后面可以在管理中下载证书,下载tomcat对应的证书(一个*.pfx文件和*.txt文件) 2.将pfx文件拷贝 ...