Skip to content

Commit 7748da3

Browse files
committed
+ problem 662
1 parent 25f98e1 commit 7748da3

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 662. Maximum Width of Binary Tree
2+
Given the `root` of a binary tree, return *the **maximum width** of the given tree*.
3+
4+
The **maximum width** of a tree is the maximum **width** among all levels.
5+
6+
The **width** of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
7+
8+
It is **guaranteed** that the answer will in the range of a **32-bit** signed integer.
9+
10+
#### Example 1:
11+
![](https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg)
12+
<pre>
13+
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
14+
<strong>Output:</strong> 4
15+
<strong>Explanation:</strong> The maximum width exists in the third level with length 4 (5,3,null,9).
16+
</pre>
17+
18+
#### Example 2:
19+
![](https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg)
20+
<pre>
21+
<strong>Input:</strong> root = [1,3,2,5,null,null,9,6,null,7]
22+
<strong>Output:</strong> 7
23+
<strong>Explanation:</strong> The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
24+
</pre>
25+
26+
#### Example 3:
27+
![](https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg)
28+
<pre>
29+
<strong>Input:</strong> root = [1,3,2,5]
30+
<strong>Output:</strong> 2
31+
<strong>Explanation:</strong> The maximum width exists in the second level with length 2 (3,2).
32+
</pre>
33+
34+
#### Constraints:
35+
* The number of nodes in the tree is in the range `[1, 3000]`.
36+
* `-100 <= Node.val <= 100`
37+
38+
## Solutions (Python)
39+
40+
### 1. Solution
41+
```Python
42+
# Definition for a binary tree node.
43+
# class TreeNode:
44+
# def __init__(self, val=0, left=None, right=None):
45+
# self.val = val
46+
# self.left = left
47+
# self.right = right
48+
class Solution:
49+
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
50+
currlevel = [(root, 0)]
51+
ret = 1
52+
53+
while currlevel != []:
54+
nextlevel = []
55+
ret = max(ret, currlevel[-1][1] - currlevel[0][1] + 1)
56+
57+
for node, x in currlevel:
58+
if node.left is not None:
59+
nextlevel.append((node.left, x << 1))
60+
if node.right is not None:
61+
nextlevel.append((node.right, (x << 1) + 1))
62+
63+
currlevel = nextlevel
64+
65+
return ret
66+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 662. 二叉树最大宽度
2+
给你一棵二叉树的根节点 `root` ,返回树的 **最大宽度**
3+
4+
树的 **最大宽度** 是所有层中最大的 **宽度**
5+
6+
每一层的 **宽度** 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些延伸到这一层的 `null` 节点,这些 `null` 节点也计入长度。
7+
8+
题目数据保证答案将会在 **32 位** 带符号整数范围内。
9+
10+
#### 示例 1:
11+
![](https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg)
12+
<pre>
13+
<strong>输入:</strong> root = [1,3,2,5,3,null,9]
14+
<strong>输出:</strong> 4
15+
<strong>解释:</strong> 最大宽度出现在树的第 3 层,宽度为 4 (5,3,null,9) 。
16+
</pre>
17+
18+
#### 示例 2:
19+
![](https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg)
20+
<pre>
21+
<strong>输入:</strong> root = [1,3,2,5,null,null,9,6,null,7]
22+
<strong>输出:</strong> 7
23+
<strong>解释:</strong> 最大宽度出现在树的第 4 层,宽度为 7 (6,null,null,null,null,null,7) 。
24+
</pre>
25+
26+
#### 示例 3:
27+
![](https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg)
28+
<pre>
29+
<strong>输入:</strong> root = [1,3,2,5]
30+
<strong>输出:</strong> 2
31+
<strong>解释:</strong> 最大宽度出现在树的第 2 层,宽度为 2 (3,2) 。
32+
</pre>
33+
34+
#### 提示:
35+
* 树中节点的数目范围是 `[1, 3000]`
36+
* `-100 <= Node.val <= 100`
37+
38+
## 题解 (Python)
39+
40+
### 1. 题解
41+
```Python
42+
# Definition for a binary tree node.
43+
# class TreeNode:
44+
# def __init__(self, val=0, left=None, right=None):
45+
# self.val = val
46+
# self.left = left
47+
# self.right = right
48+
class Solution:
49+
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
50+
currlevel = [(root, 0)]
51+
ret = 1
52+
53+
while currlevel != []:
54+
nextlevel = []
55+
ret = max(ret, currlevel[-1][1] - currlevel[0][1] + 1)
56+
57+
for node, x in currlevel:
58+
if node.left is not None:
59+
nextlevel.append((node.left, x << 1))
60+
if node.right is not None:
61+
nextlevel.append((node.right, (x << 1) + 1))
62+
63+
currlevel = nextlevel
64+
65+
return ret
66+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
9+
currlevel = [(root, 0)]
10+
ret = 1
11+
12+
while currlevel != []:
13+
nextlevel = []
14+
ret = max(ret, currlevel[-1][1] - currlevel[0][1] + 1)
15+
16+
for node, x in currlevel:
17+
if node.left is not None:
18+
nextlevel.append((node.left, x << 1))
19+
if node.right is not None:
20+
nextlevel.append((node.right, (x << 1) + 1))
21+
22+
currlevel = nextlevel
23+
24+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
[657][657l] |[Robot Return to Origin][657] |![py]&nbsp;&nbsp;![rs]
402402
[658][658l] |[Find K Closest Elements][658] |![rs]
403403
[661][661l] |[Image Smoother][661] |![rs]
404+
[662][662l] |[Maximum Width of Binary Tree][662] |![py]
404405
[665][665l] |[Non-decreasing Array][665] |![py]
405406
[669][669l] |[Trim a Binary Search Tree][669] |![py]
406407
[670][670l] |[Maximum Swap][670] |![rs]
@@ -1802,6 +1803,7 @@
18021803
[657]:Problemset/0657-Robot%20Return%20to%20Origin/README.md#657-robot-return-to-origin
18031804
[658]:Problemset/0658-Find%20K%20Closest%20Elements/README.md#658-find-k-closest-elements
18041805
[661]:Problemset/0661-Image%20Smoother/README.md#661-image-smoother
1806+
[662]:Problemset/0662-Maximum%20Width%20of%20Binary%20Tree/README.md#662-maximum-width-of-binary-tree
18051807
[665]:Problemset/0665-Non-decreasing%20Array/README.md#665-non-decreasing-array
18061808
[669]:Problemset/0669-Trim%20a%20Binary%20Search%20Tree/README.md#669-trim-a-binary-search-tree
18071809
[670]:Problemset/0670-Maximum%20Swap/README.md#670-maximum-swap
@@ -3201,6 +3203,7 @@
32013203
[657l]:https://leetcode.com/problems/robot-return-to-origin/
32023204
[658l]:https://leetcode.com/problems/find-k-closest-elements/
32033205
[661l]:https://leetcode.com/problems/image-smoother/
3206+
[662l]:https://leetcode.com/problems/maximum-width-of-binary-tree/
32043207
[665l]:https://leetcode.com/problems/non-decreasing-array/
32053208
[669l]:https://leetcode.com/problems/trim-a-binary-search-tree/
32063209
[670l]:https://leetcode.com/problems/maximum-swap/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
[657][657l] |[机器人能否返回原点][657] |![py]&nbsp;&nbsp;![rs]
402402
[658][658l] |[找到 K 个最接近的元素][658] |![rs]
403403
[661][661l] |[图片平滑器][661] |![rs]
404+
[662][662l] |[二叉树最大宽度] |![py]
404405
[665][665l] |[非递减数列][665] |![py]
405406
[669][669l] |[修剪二叉搜索树][669] |![py]
406407
[670][670l] |[最大交换][670] |![rs]
@@ -1802,6 +1803,7 @@
18021803
[657]:Problemset/0657-Robot%20Return%20to%20Origin/README_CN.md#657-机器人能否返回原点
18031804
[658]:Problemset/0658-Find%20K%20Closest%20Elements/README_CN.md#658-找到-k-个最接近的元素
18041805
[661]:Problemset/0661-Image%20Smoother/README_CN.md#661-图片平滑器
1806+
[662]:Problemset/0662-Maximum%20Width%20of%20Binary%20Tree/README_CN.md#662-二叉树最大宽度
18051807
[665]:Problemset/0665-Non-decreasing%20Array/README_CN.md#665-非递减数列
18061808
[669]:Problemset/0669-Trim%20a%20Binary%20Search%20Tree/README_CN.md#669-修剪二叉搜索树
18071809
[670]:Problemset/0670-Maximum%20Swap/README_CN.md#670-最大交换
@@ -3201,6 +3203,7 @@
32013203
[657l]:https://leetcode.cn/problems/robot-return-to-origin/
32023204
[658l]:https://leetcode.cn/problems/find-k-closest-elements/
32033205
[661l]:https://leetcode.cn/problems/image-smoother/
3206+
[662l]:https://leetcode.cn/problems/maximum-width-of-binary-tree/
32043207
[665l]:https://leetcode.cn/problems/non-decreasing-array/
32053208
[669l]:https://leetcode.cn/problems/trim-a-binary-search-tree/
32063209
[670l]:https://leetcode.cn/problems/maximum-swap/

0 commit comments

Comments
 (0)