Leetcode 662.二叉树最大宽度
二叉树最大宽度
给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与满二叉树(full binary tree)结构相同,但一些节点为空。
每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null节点也计入长度)之间的长度。
示例 1:
输入:

输出: 4
解释: 最大值出现在树的第 3 层,宽度为 4 (5,3,null,9)。
示例 2:
输入:

输出: 2
解释: 最大值出现在树的第 3 层,宽度为 2 (5,3)。
示例 3:
输入:

输出: 2
解释: 最大值出现在树的第 2 层,宽度为 2 (3,2)。
示例 4:
输入:

输出: 8
解释: 最大值出现在树的第 4 层,宽度为 8 (6,null,null,null,null,null,null,7)。
注意: 答案在32位有符号整数的表示范围内。
思路
pproach Framework
Explanation
As we need to reach every node in the given tree, we will have to traverse the tree, either with a depth-first search, or with a breadth-first search.
The main idea in this question is to give each node a position value. If we go down the left neighbor, then position -> position * 2; and if we go down the right neighbor, then position -> position * 2 + 1. This makes it so that when we look at the position values L and R of two nodes with the same depth, the width will be R - L + 1.
Intuition and Algorithm
Traverse each node in breadth-first order, keeping track of that node's position. For each depth, the first node reached is the left-most, while the last node reached is the right-most.
class Solution {
public int widthOfBinaryTree(TreeNode root) {
Queue<AnnotatedNode> queue = new LinkedList();
queue.add(new AnnotatedNode(root, 0, 0));
int curDepth = 0, left = 0, ans = 0;
while (!queue.isEmpty()) {
AnnotatedNode a = queue.poll();
if (a.node != null) {
queue.add(new AnnotatedNode(a.node.left, a.depth + 1, a.pos * 2));
queue.add(new AnnotatedNode(a.node.right, a.depth + 1, a.pos * 2 + 1));
if (curDepth != a.depth) {
curDepth = a.depth;
left = a.pos;
}
ans = Math.max(ans, a.pos - left + 1);
}
}
return ans;
}
}
class AnnotatedNode {
TreeNode node;
int depth, pos;
AnnotatedNode(TreeNode n, int d, int p) {
node = n;
depth = d;
pos = p;
}
}

Leetcode 662.二叉树最大宽度的更多相关文章
- Java实现 LeetCode 662 二叉树最大宽度(递归)
662. 二叉树最大宽度 给定一个二叉树,编写一个函数来获取这个树的最大宽度.树的宽度是所有层中的最大宽度.这个二叉树与满二叉树(full binary tree)结构相同,但一些节点为空. 每一层的 ...
- LeetCode:二叉树的锯齿形层次遍历【103】
LeetCode:二叉树的锯齿形层次遍历[103] 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 ...
- AC日记——二叉树最大宽度和高度 1501 codevs
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽 ...
- Codevs 1051 二叉树最大宽度和高度
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽 ...
- codevs1501 二叉树最大宽度和高度
难度等级:白银 1501 二叉树最大宽度和高度 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整数n. 下面 ...
- 求二叉树的宽度C语言版
/*层次遍历二叉树,每一层遍历完成以后都重新插入特定的指针 (比如本例使用的特殊指针是数据元素为#,左右儿子为空的指针), 这样在每次访问到所指向数据为#的队列中的结点指针是就知道该指针是这层的末尾, ...
- 二叉树的宽度<java版>
二叉树的宽度 思路:层序遍历的时候,记录每层的节点数量,最后取记录中的最多的数量. 代码实现: public int solution(TreeNode node){ LinkedList<Tr ...
- LeetCode 107 ——二叉树的层次遍历 II
1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...
- LeetCode:二叉树的后序遍历【145】
LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
随机推荐
- 2018.9.7 ArrayList
ArrayList简介 ArrayList核心源码 ArrayList源码分析 System.arraycopy()和Arrays.copyOf()方法 两者联系与区别 ArrayList核心扩容技术 ...
- scrapy Pipeline 练习
class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...
- Android笔记(预安装APK)
一般一个安卓的产品在出厂时,会预安装许多APK,关于这些APP,主要分为下面这几类 1.系统级别APK 这一类应用一般是:电话/设置或者厂家自己特定的应用. 2.系统预安装APK 因为商业原因,产品出 ...
- maven中import scope依赖方式解决单继承问题的理解
在maven多模块项目中,为了保持模块间依赖的统一,常规做法是在parent model中,使用dependencyManagement预定义所有模块需要用到的dependency(依赖) <d ...
- 在ubuntu中docker的简单使用(一)
>>docker version 当运行docker version 命令出现Cannot connect to Docker daemon. Is the docker daemon r ...
- JAVA文件操作工具类(读、增、删除、复制)
使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...
- python 计算提成代码
while True: with open('8564.txt') as f: r = f.readlines() start = input("请输入要查询的日期,例如20180101 : ...
- centos 7忘记了root密码,如何改密码?
今天服务器突然进不去了,不知道是密码被改了,还是什么情况! 服务器版本:centos 7.0 网上查找了很多文档,有些办法不可行,如果是亲自试过绝对可行的方法: 1:重启服务器,如下界面,按键盘 &q ...
- logrus学习笔记
logrus源码:https://github.com/sirupsen/logrus 1.logrus.Entry结构 1.1 类型 type Entry struct { Logger *Logg ...
- Ajax上传文件/照片时报错TypeError :Illegal invocation
问题 Ajax上传文件/照片时报错TypeError :Illegal invocation 解决 网上搜索问题,错误原因可能有以下几个,依次检查: 请求类型有误,如post请求,但在后台设置的是ge ...