[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 ...
随机推荐
- odoo13之右上角弹出提示框
前言 在odoo中已经提供好了右上角弹出提示框的接口,我们只需要调用即可: 而提示框的实现又分为前端js实现和后段函数实现,前后端实现的效果相同. 实现效果图 前端实现提示框 在前端中显示提示框最常用 ...
- 对象不支持“assign”属性或方法
1. 报错信息 vue项目打包部署后,ie11报错内容如下: 看到报错信息肯定是语法兼容问题了,经测试 Edge 无此情况,部分ie9也有此类问题. 2. 尝试方法 安装 create-react-a ...
- charles 常用功能(八)重定向
1.点击鼠标右键 点击保存就保存到桌面上了 效果图 在123.txt中修改 然后另存为 点击红圈处 然后再次发送请求
- PyQt学习随笔:QTableWidget的visualRow、visualColumn、logicalRow、logicalColumn(可见行、逻辑行、可见列、逻辑列)相关概念及方法探究
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概念 关于逻辑行logicalRow.列logicalColumn和可见行visualRow.列 ...
- PyQt学习随笔:ListView控件的视图和数据模型分离案例
Qt 中view类控件的目的是实现数据和模型分离,控件展示数据,数据保存在数据存储中,数据存储中的数据改变了,则控件中展示的数据跟随改变.当设计时只指定了一个控件和一个数据存储关联时,这种分离虽然也能 ...
- tensorflow 小记——如何对张量做任意行求和,得到新tensor(一种方法:列表生成式)
希望实现图片上的功能 import tensorflow as tfa = tf.range(10,dtype=float)b = aa = tf.reshape(a,[-1,1])a = tf.ti ...
- windows+jenkins+iis 部署
1.安装jenkins 下载地址:https://www.jenkins.io/download/ 2.需要配置java环境 配置教程:https://www.cnblogs.com/liuxiaoj ...
- 第 7篇 Scrum 冲刺博客
一.站立式会议 1.站立式会议照片 2.昨天已完成的工作 对职工的查询 3.今天计划完成的工作 继续与同学对接,争取早日完成项目的整个流程 初步对数据库筛选 4.工作中遇到的困难 ①有同学不知道如何远 ...
- git学习——git下载安装
原文来至 一.集中式vs分布式 Linus一直痛恨的CVS及SVN都是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢? 先说集中式版本控制系统,版本库是集中 ...
- 架构师基础技能-搭建gitLab
前言 想要成为一名架构师,一定要有从无到有搭建环境的能力,这是作为架构师的基础技能,而gitLab服务器的搭建一定又是重中之重. 相信很多小伙伴的公司也在使用gitLab,但都是你们公司的架构师搭建好 ...