[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 tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
Example 1:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Input:
1
/
3
/ \
5 3
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Input:
1
/ \
3 2
/
5
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:
Input:
1
/ \
3 2
/ \
5 9
/ \
6 7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
Note: Answer will in the range of 32-bit signed integer.
这道题让我们求二叉树的最大宽度,根据题目中的描述可知,这里的最大宽度不是满树的时候的最大宽度,如果是那样的话,肯定是最后一层的结点数最多。这里的最大宽度应该是两个存在的结点中间可容纳的总的结点个数,中间的结点可以为空。那么其实只要我们知道了每一层中最左边和最右边的结点的位置,我们就可以算出这一层的宽度了。所以这道题的关键就是要记录每一层中最左边结点的位置,我们知道对于一棵完美二叉树,如果根结点是深度1,那么每一层的结点数就是 2*n-1,那么每个结点的位置就是 [1, 2*n-1] 中的一个,假设某个结点的位置是i,那么其左右子结点的位置可以直接算出来,为 2*i 和 2*i+1,可以自行带例子检验。由于之前说过,我们需要保存每一层的最左结点的位置,那么我们使用一个数组 start,由于数组是从0开始的,我们就姑且认定根结点的深度为0,不影响结果。我们从根结点进入,深度为0,位置为1,进入递归函数。首先判断,如果当前结点为空,那么直接返回,然后判断如果当前深度大于 start 数组的长度,说明当前到了新的一层的最左结点,我们将当前位置存入 start 数组中。然后我们用 idx - start[h] + 1 来更新结果 res。这里 idx 是当前结点的位置,start[h] 是当前层最左结点的位置。然后对左右子结点分别调用递归函数,注意左右子结点的位置可以直接计算出来,代码参见评论区二楼。我们也可以让递归函数直接返回最大宽度了,但是解题思路没有啥区别,代码参见评论区三楼。这两种方法之前都能通过 OJ,直到后来加了一个很极端的 test case,使得二者都整型溢出了 Signed Integer Overflow,原因是这里每层都只有1个结点,而我们代码中坐标每次都要乘以2的,所以到32层以后就直接溢出了。为了避免溢出的问题,需要做些优化,就是要统计每层的结点数,若该层只有一个结点,那么该层结点的坐标值重置为1,这样就可以避免溢出了。所以 DFS 的解法就不能用了,只能用层序遍历,迭代的方法来写,这里使用了队列 queue 来辅助运算,queue 里存的是一个 pair,结点和其当前位置,在进入新一层的循环时,首先要判断该层是否只有1个结点,是的话重置结点坐标位置,再将首结点的位置保存出来当作最左位置,然后对于遍历到的结点,都更新右结点的位置,遍历一层的结点后来计算宽度更新结果 res,参见代码如下:
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
if (!root) return ;
int res = ;
queue<pair<TreeNode*,int>> q;
q.push({root, });
while (!q.empty()) {
if (q.size() == ) q.front().second = ;
int left = q.front().second, right = left, n = q.size();
for (int i = ; i < n; ++i) {
auto t = q.front().first;
right = q.front().second; q.pop();
if (t->left) q.push({t->left, right * });
if (t->right) q.push({t->right, right * + });
}
res = max(res, right - left + );
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/662
参考资料:
https://leetcode.com/problems/maximum-width-of-binary-tree/
https://leetcode.com/problems/maximum-width-of-binary-tree/discuss/327721/cpp-bfs-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode]662. Maximum Width of Binary Tree判断树的宽度
public int widthOfBinaryTree(TreeNode root) { /* 层序遍历+记录完全二叉树的坐标,左孩子2*i,右孩子2*i+1 而且要有两个变量,一个记录本层节点数, ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- [LeetCode] 543. Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- 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 ...
随机推荐
- grep -vFf 比较2个文件差异
grep -vFf 1.txt 2.txt 打印出2.txt中与1.txt有差异的数据. #cat .txt 192.168.0.1 192.168.0.2 192.168.0.3 #cat .t ...
- 程序猿媛 九:Adroid zxing 二维码3.1集成(源码无删减)
Adroid zxing 二维码3.1集成 声明:博文为原创,文章内容为,效果展示,思路阐述,及代码片段. 转载请保留原文出处“http://my.oschina.net/gluoyer/blog”, ...
- Python中的unittest和logging
今天使用Python的unittest模块写了些单元测试,现记录下要点: 使用unittest的基本格式如下: import unittest class Test(unittest.TestCase ...
- Notepad++使用vs2015主题教程
前言: 最近几天都在用Notepad++,所以想换个看得舒服点的主题. 发现vs2015的主题颜色特别好看.所以就查了一下有没有大佬做了这样的Notepad++主题. 结果是有的. 正文: notep ...
- Java虚拟机之类加载机制
⑴背景 Java虚拟机把Class文件加载到内存中,并对数据进行校验,转换解析,和初始化,最终形成被虚拟机直接使用的Java类型,这就是类加载机制. ⑵Jvm加载Class文件机制原理 类的生命周 ...
- 利用1.1.1.1进行DNS网络加速,仅需2分钟让网络更快
NEWS 近日,Cloudflare 和 APNIC联合推出了1.1.1.1DNS网络加速. Cloudflare 运行全球规模最大.速度最快的网络之一.APNIC 是一个非营利组织,管理着亚太和大洋 ...
- I Know Alpha冲刺随笔集
Alpha冲刺 Day1 Alpha冲刺 Day2 Alpha冲刺 Day3 Alpha冲刺 Day4 Alpha冲刺 Day5 Alpha冲刺 Day6 Alpha冲刺 Day7 Alpha冲刺 D ...
- Beta冲刺 第四天
Beta冲刺 第四天 1. 昨天的困难 1.网页使用了一些网上现成的模板,其主要是使用像素做处理的,所以检查起来比较费事费力. 2.使用github代码merge时出现了问题.所以花费了不少的时间在人 ...
- 项目Alpha冲刺Day6
一.会议照片 二.项目进展 1.今日安排 熟悉后台框架并编写.继续搭建前台框架模版.熟悉前端框架开发流程.完成前端热部署配置.完成部分后台用户信息相关接口.解决后台jdk1.8日期在框架中的使用. 2 ...
- const volatile同时限定一个类型int a = 10
const和volatile放在一起的意义在于: (1)本程序段中不能对a作修改,任何修改都是非法的,或者至少是粗心,编译器应该报错,防止这种粗心: (2)另一个程序段则完全有可能修改,因此编译器最好 ...