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] 进阶: 递归算法很 ...
随机推荐
- 【转】在程序中设置android:gravity 和 android:layout_Gravity属性
在进行UI布局的时候,可能经常会用到 android:gravity 和 android:layout_Gravity 这两个属性. 关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一 ...
- 利用python中的PIL进行矩阵与图像之间的转换
1.图像转换为矩阵 matrix = numpy.asarray(image) 2.矩阵转换为图像 image = Image.fromarray(matrix)
- 卷积神经网络CNN理解
自今年七月份以来,一直在实验室负责卷积神经网络(Convolutional Neural Network,CNN),期间配置和使用过theano和cuda-convnet.cuda-convnet2. ...
- 获取页面的url
设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...
- php curl使用总结(一)
今天和第三方支付做对接的时候,在本地用wamp(php版本5.4.14)运行他们的支付demo的时候,报了一个错误.loadXML函数中不能传空值.排查代码的时候,发现他们用了curl,我以前也接触过 ...
- 注解@Component,@Controller,@Service,@Repository简单了解
Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring B ...
- OC#import和#include的异同
1.#import和#include相同1.1都可以用在OC程序中起到导入文件的作用1.2同样的 包含系统文件都是<>,是包本地文件都用""例如:系统文件#import ...
- 第43章 RTC—实时时钟—零死角玩转STM32-F429系列
第43章 RTC—实时时钟 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fireg ...
- Eclipse使用的小技巧
1.在右键new菜单栏中添加新建JSP文件 window->perspective->customize perspective->shortcuts->web->把JS ...
- ant Design表单验证笔记
1.pattern正则验证 <Col md={12} sm={24}> <FormItem {...formItemLayout} label="班数"> ...