Skip to content

Commit d5d1e67

Browse files
committed
一刷662
1 parent 16ce954 commit d5d1e67

File tree

11 files changed

+177
-67
lines changed

11 files changed

+177
-67
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4659,14 +4659,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
46594659
//|{doc_base_url}/0661-image-smoother.adoc[题解]
46604660
//|Easy
46614661
//|
4662-
//
4663-
//|{counter:codes}
4664-
//|{leetcode_base_url}/maximum-width-of-binary-tree/[662. Maximum Width of Binary Tree^]
4665-
//|{source_base_url}/_0662_MaximumWidthOfBinaryTree.java[Java]
4666-
//|{doc_base_url}/0662-maximum-width-of-binary-tree.adoc[题解]
4667-
//|Medium
4668-
//|
4669-
//
4662+
4663+
|{counter:codes}
4664+
|{leetcode_base_url}/maximum-width-of-binary-tree/[662. Maximum Width of Binary Tree^]
4665+
|{source_base_url}/_0662_MaximumWidthOfBinaryTree.java[Java]
4666+
|{doc_base_url}/0662-maximum-width-of-binary-tree.adoc[题解]
4667+
|Medium
4668+
|
4669+
46704670
//|{counter:codes}
46714671
//|{leetcode_base_url}/equal-tree-partition/[663. Equal Tree Partition^]
46724672
//|{source_base_url}/_0663_EqualTreePartition.java[Java]
Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,99 @@
11
[#0662-maximum-width-of-binary-tree]
2-
= 662. Maximum Width of Binary Tree
2+
= 662. 二叉树最大宽度
33

4-
{leetcode}/problems/maximum-width-of-binary-tree/[LeetCode - Maximum Width of Binary Tree^]
4+
https://leetcode.cn/problems/maximum-width-of-binary-tree/[LeetCode - 662. 二叉树最大宽度 ^]
55

6-
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.
6+
给你一棵二叉树的根节点 `root` ,返回树的 *最大宽度*
77

8-
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.
8+
树的 *最大宽度* 是所有层中最大的 *宽度*
99

10-
*Example 1:*
10+
每一层的 *宽度* 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些延伸到这一层的 `null` 节点,这些 `null` 节点也计入长度。
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:*
12+
题目数据保证答案将会在 *32 位* 带符号整数范围内。
1513

16-
1
17-
/ \
18-
3 2
19-
/ \ \
20-
5 3 9
14+
*示例 1:*
2115

22-
*Output:* 4
23-
*Explanation:* The maximum width existing in the third level with the length 4 (5,3,null,9).
24-
----
16+
image::images/0662-01.jpg[{image_attr}]
2517

26-
*Example 2:*
18+
....
19+
输入:root = [1,3,2,5,3,null,9]
20+
输出:4
21+
解释:最大宽度出现在树的第 3 层,宽度为 4 (5,3,null,9) 。
22+
....
2723

28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:*
24+
*示例 2:*
3125

32-
1
33-
/
34-
3
35-
/ \
36-
5 3
26+
image::images/0662-02.jpg[{image_attr}]
3727

38-
*Output:* 2
39-
*Explanation:* The maximum width existing in the third level with the length 2 (5,3).
40-
----
28+
....
29+
输入:root = [1,3,2,5,null,null,9,6,null,7]
30+
输出:7
31+
解释:最大宽度出现在树的第 4 层,宽度为 7 (6,null,null,null,null,null,7) 。
32+
....
4133

42-
*Example 3:*
34+
*示例 3:*
4335

44-
[subs="verbatim,quotes,macros"]
45-
----
46-
*Input:*
36+
image::images/0662-03.jpg[{image_attr}]
4737

48-
1
49-
/ \
50-
3 2
51-
/
52-
5
38+
....
39+
输入:root = [1,3,2,5]
40+
输出:2
41+
解释:最大宽度出现在树的第 2 层,宽度为 2 (3,2) 。
42+
....
5343

54-
*Output:* 2
55-
*Explanation:* The maximum width existing in the second level with the length 2 (3,2).
56-
----
44+
*提示:*
5745

58-
*Example 4:*
46+
* 树中节点的数目范围是 `[1, 3000]`
47+
* `+-100 <= Node.val <= 100+`
5948
60-
[subs="verbatim,quotes,macros"]
61-
----
62-
*Input:*
6349
64-
1
65-
/ \
66-
3 2
67-
/ \
68-
5 9
69-
/ \
70-
6 7
71-
*Output:* 8
72-
*Explanation:* The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
50+
== 思路分析
7351

52+
使用伪节点进行广度优先遍历超时!
7453

75-
----
54+
从上到下,每个节点都有一个编号,如果父节点是 `x`,则左节点是 `2x`,而右节点是 `2x+1`,因为父节点可以这个节点编号传递下去,这样不需要关注空节点。把每一层节点放入到一个队列中,尾部节点的编号减去头部节点编号再加 1 就是每层的宽度。
7655

77-
*Note:* Answer will in the range of 32-bit signed integer.
56+
image::images/0662-10.png[{image_attr}]
7857

58+
还有一个深度优先遍历的解法:
7959

60+
image::images/0662-11.png[{image_attr}]
8061

8162
[[src-0662]]
63+
[tabs]
64+
====
65+
一刷(超时)::
66+
+
67+
--
8268
[{java_src_attr}]
8369
----
8470
include::{sourcedir}/_0662_MaximumWidthOfBinaryTree.java[tag=answer]
8571
----
72+
--
73+
74+
一刷::
75+
+
76+
--
77+
[{java_src_attr}]
78+
----
79+
include::{sourcedir}/_0662_MaximumWidthOfBinaryTree_1.java[tag=answer]
80+
----
81+
--
82+
83+
// 二刷::
84+
// +
85+
// --
86+
// [{java_src_attr}]
87+
// ----
88+
// include::{sourcedir}/_0662_MaximumWidthOfBinaryTree_2.java[tag=answer]
89+
// ----
90+
// --
91+
====
92+
93+
94+
== 参考资料
95+
96+
. https://leetcode.cn/problems/maximum-width-of-binary-tree/solutions/1776589/er-cha-shu-zui-da-kuan-du-by-leetcode-so-9zp3/[662. 二叉树最大宽度 - 官方题解^]
97+
. https://leetcode.cn/problems/maximum-width-of-binary-tree/solutions/1779284/by-muse-77-7hwf/[662. 二叉树最大宽度 - 【爪哇缪斯】图解LeetCode^]
98+
8699

docs/images/0662-01.jpg

12.6 KB
Loading

docs/images/0662-02.jpg

19.8 KB
Loading

docs/images/0662-03.jpg

8.44 KB
Loading

docs/images/0662-10.png

226 KB
Loading

docs/images/0662-11.png

417 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ include::0654-maximum-binary-tree.adoc[leveloffset=+1]
14061406

14071407
// include::0661-image-smoother.adoc[leveloffset=+1]
14081408

1409-
// include::0662-maximum-width-of-binary-tree.adoc[leveloffset=+1]
1409+
include::0662-maximum-width-of-binary-tree.adoc[leveloffset=+1]
14101410

14111411
// include::0663-equal-tree-partition.adoc[leveloffset=+1]
14121412

logbook/202503.adoc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,21 +363,26 @@ endif::[]
363363
|{doc_base_url}/0011-container-with-most-water.adoc[题解]
364364
|✅ 双指针
365365

366-
|{counter:codes}
366+
|{counter:codes2503}
367367
|{leetcode_base_url}/remove-element/[27. 移除元素^]
368368
|{doc_base_url}/0027-remove-element.adoc[题解]
369369
|✅ 快慢指针。慢指针记录满足条件的长度,快指针指向需要处理的元素。
370370

371-
|{counter:codes}
371+
|{counter:codes2503}
372372
|{leetcode_base_url}/trapping-rain-water/[42. 接雨水^]
373373
|{doc_base_url}/0042-trapping-rain-water.adoc[题解]
374374
|❌ 使用单调栈没写出来。使用单调栈,需 `h × w`,不能只看头顶的容量。使用左右双指针夹逼代码更简单,只需要计算头顶的容量即可。
375375

376-
|{counter:codes}
376+
|{counter:codes2503}
377377
|{leetcode_base_url}/edit-distance/[72. 编辑距离^]
378378
|{doc_base_url}/0072-edit-distance.adoc[题解]
379379
|✅ 动态规划。思考如何进一步优化成一维数组?
380380

381+
|{counter:codes2503}
382+
|{leetcode_base_url}/maximum-width-of-binary-tree/[662. Maximum Width of Binary Tree^]
383+
|{doc_base_url}/0662-maximum-width-of-binary-tree.adoc[题解]
384+
|⭕️ 添加虚拟节点的做法超时。看答案给节点加编号通过。
385+
381386
|===
382387

383388
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
import java.util.Deque;
6+
import java.util.LinkedList;
7+
8+
public class _0662_MaximumWidthOfBinaryTree {
9+
// tag::answer[]
10+
/**
11+
* 通过 94 / 116 个测试用例,但超时了。
12+
*
13+
* @author D瓜哥 · https://www.diguage.com
14+
* @since 2025-04-20 21:13:45
15+
*/
16+
public int widthOfBinaryTree(TreeNode root) {
17+
final TreeNode DUMMY = new TreeNode(Integer.MAX_VALUE);
18+
Deque<TreeNode> queue = new LinkedList<>();
19+
queue.offer(root);
20+
int result = 0;
21+
while (!queue.isEmpty()) {
22+
int size = queue.size();
23+
result = Math.max(result, size);
24+
for (int i = 0; i < size; i++) {
25+
TreeNode node = queue.poll();
26+
if (node.left != null) {
27+
queue.offer(node.left);
28+
} else {
29+
queue.offer(DUMMY);
30+
}
31+
if (node.right != null) {
32+
queue.offer(node.right);
33+
} else {
34+
queue.offer(DUMMY);
35+
}
36+
}
37+
while (!queue.isEmpty() && queue.peek() == DUMMY) {
38+
queue.poll();
39+
}
40+
while (!queue.isEmpty() && queue.peekLast() == DUMMY) {
41+
queue.pollLast();
42+
}
43+
}
44+
return result;
45+
}
46+
// end::answer[]
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
import java.util.Deque;
6+
import java.util.LinkedList;
7+
8+
public class _0662_MaximumWidthOfBinaryTree_1 {
9+
// tag::answer[]
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2025-04-20 21:37:22
13+
*/
14+
public int widthOfBinaryTree(TreeNode root) {
15+
Deque<Pair<TreeNode, Integer>> nodes = new LinkedList<>();
16+
int result = 0;
17+
nodes.add(new Pair<>(root, 1));
18+
while (!nodes.isEmpty()) {
19+
result = Math.max(result,
20+
nodes.getLast().value - nodes.getFirst().value + 1);
21+
int size = nodes.size();
22+
for (int i = 0; i < size; i++) {
23+
Pair<TreeNode, Integer> pair = nodes.poll();
24+
if (pair.key.left != null) {
25+
nodes.offer(new Pair<>(pair.key.left, pair.value * 2));
26+
}
27+
if (pair.key.right != null) {
28+
nodes.offer(new Pair<>(pair.key.right, pair.value * 2 + 1));
29+
}
30+
}
31+
}
32+
return result;
33+
}
34+
35+
private static class Pair<K, V> {
36+
K key;
37+
V value;
38+
39+
public Pair(K key, V value) {
40+
this.key = key;
41+
this.value = value;
42+
}
43+
}
44+
// end::answer[]
45+
}

0 commit comments

Comments
 (0)