[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 ...
随机推荐
- UPX使用教程
UPX是一个通用可执行文件压缩器,由于其具有: 压缩率高:压缩效果优于zip/gzip: 解压速度快:在奔腾133上即可达到大约10MB/秒: 压缩的可执行文件没有额外的内存开销: 安全:可以列表,检 ...
- C#(二)基础篇—操作符
2020-12-02 本随笔为个人复习巩固知识用,多从书上总结与理解得来,如有错误麻烦指正 1.数学操作符 int a=2,b=3,c=0; float d=0; c=a+b; //c=5 c++; ...
- springboot补充
springboot中的日志: 在默认的spring-boot-starter中,会引入spring-boot-starter-logging, 而springboot-starte-longing中 ...
- day6(celery配置与基本使用)
1.celery配置与基本使用 1.1 安装celery pip install celery @ https://github.com/celery/celery/tarball/master 1. ...
- PyQt(Python+Qt)学习随笔:QDockWidget停靠窗相关的信号
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 QDockWidget的信号包括与属性变更相关的allowedArea ...
- PyQt(Python+Qt)学习随笔:QListWidget获取指定行对应项的item()方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在列表部件中,可以通过item方法获取指定行对应的项,语法如下: QListWidgetItem i ...
- 静湖ABC段扫描器(长更)
想学习python图形化的知识,从C段扫描器开始入手,很多的扫描器只能扫描C段,对于A,B段却没有设置这个功能,所以想将扫描A段和B段的功能跟C段一起做了. 暂时的想法是,先完成C段扫描器,扫描的同时 ...
- 团队作业part4--项目冲刺
七天敏捷冲刺汇总 1. Day1 Scrum 冲刺博客 2. Day2 Scrum 冲刺博客 3. Day3 Scrum 冲刺博客 4. Day4 Scrum 冲刺博客 5. Day5 Scrum 冲 ...
- java视频流的断点续传功能
项目中需要实现浏览器中视频的拖动问题解决 /** * 视频文件的断点续传功能 * @param path 文件路径 * @param request request * @param response ...
- SpringBoot添加多数据源mysql和oracle
项目结构 多数据源配置文件 MultiDataSourceConfig.java SqlSessionTemplate1.java SqlSessionTemplate2.java package c ...