Skip to content

Commit 8653e79

Browse files
committed
三刷102
1 parent 2fbdd63 commit 8653e79

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

docs/0102-binary-tree-level-order-traversal.adoc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,43 @@ return its level order traversal as:
3636
]
3737
----
3838

39+
== 思路分析
40+
41+
image:images/0102-00.png[]
3942

4043
[[src-0102]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
4149
[{java_src_attr}]
4250
----
4351
include::{sourcedir}/_0102_BinaryTreeLevelOrderTraversal.java[tag=answer]
4452
----
53+
--
4554
55+
二刷::
56+
+
57+
--
4658
[{java_src_attr}]
4759
----
4860
include::{sourcedir}/_0102_BinaryTreeLevelOrderTraversal_2.java[tag=answer]
4961
----
62+
--
63+
64+
三刷::
65+
+
66+
--
67+
[{java_src_attr}]
68+
----
69+
include::{sourcedir}/_0102_BinaryTreeLevelOrderTraversal_3.java[tag=answer]
70+
----
71+
--
72+
====
5073

5174
TIP: TODO:还可以优化成只用一个队列对象,思考如何实现?
5275

53-
image:images/0102-00.png[]
5476

5577
== 拓展题
5678

@@ -59,4 +81,5 @@ image:images/0102-00.png[]
5981
== 参考资料
6082

6183
. https://cloud.tencent.com/developer/article/1875052[LeetCode通关:连刷三十九道二叉树,刷疯了!^]
84+
. https://leetcode.cn/problems/binary-tree-level-order-traversal/solutions/2361604/102-er-cha-shu-de-ceng-xu-bian-li-yan-du-dyf7/?envType=study-plan-v2&envId=selected-coding-interview[102. 二叉树的层序遍历 - 广度优先遍历,清晰图解^]
6285

logbook/202406.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,12 @@
628628
|{counter:codes}
629629
|{leetcode_base_url}/first-bad-version/[278. First Bad Version^]
630630
|{doc_base_url}/0278-first-bad-version.adoc[题解]
631-
|
631+
|✅逼近边界的二分查找
632+
633+
|{counter:codes}
634+
|{leetcode_base_url}/binary-tree-level-order-traversal/[102. Binary Tree Level Order Traversal^]
635+
|{doc_base_url}/0102-binary-tree-level-order-traversal.adoc[题解]
636+
|✅广度优先遍历
632637

633638
|===
634639

src/main/java/com/diguage/algo/leetcode/_0102_BinaryTreeLevelOrderTraversal_2.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* ----
3636
*
3737
* @author D瓜哥 · https://www.diguage.com
38-
* @since 2020-01-24 20:40
38+
* @since 2024-06-25 11:27
3939
*/
4040
public class _0102_BinaryTreeLevelOrderTraversal_2 {
4141
// tag::answer[]
@@ -44,6 +44,9 @@ public class _0102_BinaryTreeLevelOrderTraversal_2 {
4444
* Runtime: 3 ms, faster than 5.15% of Java online submissions for Binary Tree Level Order Traversal.
4545
* <p>
4646
* Memory Usage: 40.2 MB, less than 5.33% of Java online submissions for Binary Tree Level Order Traversal.
47+
*
48+
* @author D瓜哥 · https://www.diguage.com
49+
* @since 2024-06-25 11:27
4750
*/
4851
public List<List<Integer>> levelOrder(TreeNode root) {
4952
List<List<Integer>> result = new ArrayList<>();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
import java.util.*;
6+
7+
public class _0102_BinaryTreeLevelOrderTraversal_3 {
8+
// tag::answer[]
9+
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2024-06-25 11:27
13+
*/
14+
public List<List<Integer>> levelOrder(TreeNode root) {
15+
if (root == null) {
16+
return Collections.emptyList();
17+
}
18+
List<List<Integer>> result = new ArrayList<>();
19+
Queue<TreeNode> queue = new LinkedList<>();
20+
queue.offer(root);
21+
while (!queue.isEmpty()) {
22+
int size = queue.size();
23+
List<Integer> nums = new ArrayList<>(size);
24+
for (int i = 0; i < size; i++) {
25+
TreeNode node = queue.poll();
26+
nums.add(node.val);
27+
if (node.left != null) {
28+
queue.offer(node.left);
29+
}
30+
if (node.right != null) {
31+
queue.offer(node.right);
32+
}
33+
}
34+
result.add(nums);
35+
}
36+
return result;
37+
}
38+
// end::answer[]
39+
}

0 commit comments

Comments
 (0)