[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 ...
随机推荐
- (转)React学习笔记(干货满满)
1. React 长什么样 React 是 facebook 开源出来的一套前端方案,官网在 https://reactjs.org . 先看一个简单的样子: <!DOCTYPE html> ...
- 2014_07_11_VGA基础及封装
-- VR1201 Color Filter LCOS Microdisplays QVGA (320*240 Pixel) Color Filter LCOS Microdisplays -- VR ...
- MiniProfiler性能监控分析工具在.NET项目中的使用
MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控 ...
- java简单的实现搜索框的下拉显示相关搜索功能
最近做了一个简单的搜索框下面下拉显示相关搜索的功能,有点模仿百度的下拉展示相关搜索 先上个展示图 : 点击进入演示地址,大家可以输入长点的搜索,点击搜索,再输入之前搜索词的前面部分,看是否能展示出来 ...
- django+celery+redis应用
一.celery介绍 1.应用场景 a. Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理,如果你的业务场景中需要用到异步任务,就可以考虑使用ce ...
- python装饰器基础及应用
一.简介 装饰器是是修改其它函数功能的函数:其意义是让其他函数在不修改任何代码的前提下增加额外功能 二.数据类型 首先我们来看一段简单的代码: from types import MethodType ...
- Python类知识学习时的部分问题
Python的富比较方法__eq__和__ne__之间的关联关系分析 Python的富比较方法__le__.ge__之间的关联关系分析 Python的富比较方法__lt.__gt__之间的关联关系分析 ...
- kettle如何从cube抽数据
接触kettle已经还是有一段时间了,但是一直都使用简单的输入.输出(二维数据库to二维数据库).今天,突然接到一个需求,需要从多维数据库(CUBE)里面将数据抽取到二维数据库,我难住了,不知道该如何 ...
- Hello TLM
前言 目标 了解TLM程序的基本过程.TLM的英文全称是Transaction Level Modeling,中文翻译为事务级建模.它是在SystemC基础上的一个扩展库. 功能描述 模块A向模块B发 ...
- SSH无密码登陆
http://blog.chinaunix.net/uid-26284395-id-2949145.html