[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 ...
随机推荐
- LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
- BootstrapBlazor 组件库使用体验---Table篇
原文地址:https://www.cnblogs.com/ysmc/p/13323242.html Blazor 是一个使用 .NET 生成交互式客户端 Web UI 的框架: 使用 C# 代替 Ja ...
- rest-framework 响应器(渲染器)
一 作用: 根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件. 用户请求URL: http://127.0.0.1:8000/test/?format=json http ...
- 05 python开发之文件处理
05 python开发之文件处理 目录 05 python开发之文件处理 5 文件处理 5.1 字符编码 5.1.1 基本概念 5.1.2 发展历程 5.1.3 使用 5.2 文件处理基础 5.2.1 ...
- Docker实战 | 第三篇:Docker安装Nginx,实现基于vue-element-admin框架构建的项目线上部署
一. 前言 在上一文中 点击跳转 通过IDEA集成Docker插件实现微服务的一键部署,但 youlai-mall 是前后端分离的项目,除了后端微服务的部署之外,当然还少不了前端工程的部署.所以本篇讲 ...
- (七)if/else就是goto
一.CPU如何执行指令 CPU上有数以亿计的晶体管组层的复杂电路,我们先不用管具体电路如何实现:逻辑上我们可以认为CPU由许多寄存器组成,而这些寄存器又由许多锁存器和触发器组成,N个锁存器或触发器就可 ...
- Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
Python中splitlines([keepends])方法用于返回由原字符串中各行组成的列表,在行边界的位置拆分. 如果keepends=True,结果列表中包含行边界,否则不包含 行边界的字符. ...
- 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 Python中的标准输出和错误输出由sys模块的stdout.stde ...
- PyQt学习随笔:ListView控件删除一项列表项的方法
ListView控件可以通过控件对应数据存储删除列表项,具体使用: 数据存储.removeRow(元素索引位置) 删除指定位置的一个列表项. 数据存储如果不知道程序定义的数据存储名,可以通过model ...
- 安装pyspider出现的问题
本文来自微信公众号:coder_xiaobu,欢迎关注 一.安装pyspider pip install pyspider 二.启动 pyspider all 三.安装中出现的问题处理 安装的时候出现 ...