Skip to content

Commit d1875e1

Browse files
committed
三刷113
1 parent b9aaa11 commit d1875e1

File tree

5 files changed

+101
-27
lines changed

5 files changed

+101
-27
lines changed

docs/0113-path-sum-ii.adoc

+44-25
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,8 @@ Return:
3838

3939
这样要重点注意的是:回溯时,前进和后退要成对出现。
4040

41-
== 参考资料
42-
43-
. https://leetcode-cn.com/problems/path-sum-ii/solution/dfs-by-powcai-2/[DFS - 路径总和 II - 力扣(LeetCode)^]
44-
45-
[[src-0113]]
46-
[{java_src_attr}]
47-
----
48-
include::{sourcedir}/_0113_PathSumII.java[tag=answer]
49-
----
50-
51-
== 未优化版
52-
53-
[{java_src_attr}]
54-
----
55-
include::{sourcedir}/_0113_PathSumII_2.java[tag=answer]
56-
----
57-
58-
== 优化版
59-
60-
[{java_src_attr}]
61-
----
62-
include::{sourcedir}/_0113_PathSumII_21.java[tag=answer]
63-
----
64-
6541
TIP: 非常典型的回溯问题!
6642

67-
6843
image::images/0113-01.png[{image_attr}]
6944

7045
image::images/0113-02.png[{image_attr}]
@@ -89,3 +64,47 @@ image::images/0113-11.png[{image_attr}]
8964

9065
image::images/0113-12.png[{image_attr}]
9166

67+
[[src-0113]]
68+
[tabs]
69+
====
70+
一刷::
71+
+
72+
--
73+
[{java_src_attr}]
74+
----
75+
include::{sourcedir}/_0113_PathSumII.java[tag=answer]
76+
----
77+
--
78+
79+
二刷(待优化)::
80+
+
81+
--
82+
[{java_src_attr}]
83+
----
84+
include::{sourcedir}/_0113_PathSumII_2.java[tag=answer]
85+
----
86+
--
87+
88+
二刷(优化)::
89+
+
90+
--
91+
[{java_src_attr}]
92+
----
93+
include::{sourcedir}/_0113_PathSumII_21.java[tag=answer]
94+
----
95+
--
96+
97+
三刷::
98+
+
99+
--
100+
[{java_src_attr}]
101+
----
102+
include::{sourcedir}/_0113_PathSumII_3.java[tag=answer]
103+
----
104+
--
105+
====
106+
107+
== 参考资料
108+
109+
. https://leetcode-cn.com/problems/path-sum-ii/solution/dfs-by-powcai-2/[DFS - 路径总和 II - 力扣(LeetCode)^]
110+

docs/0155-min-stack.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ include::{sourcedir}/_0155_MinStack_2.java[tag=answer]
5757
----
5858
--
5959
60-
二刷::
60+
三刷::
6161
+
6262
--
6363
[{java_src_attr}]

logbook/202406.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@
575575
|{doc_base_url}/0121-best-time-to-buy-and-sell-stock.adoc[题解]
576576
|
577577

578+
|{counter:codes}
579+
|{leetcode_base_url}/path-sum-ii/[113. Path Sum II^]
580+
|{doc_base_url}/0113-path-sum-ii.adoc[题解]
581+
|回溯
582+
578583
|===
579584

580585
截止目前,本轮练习一共完成 {codes} 道题。

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
* https://leetcode.com/problems/path-sum-ii/[Path Sum II - LeetCode]
1414
*
1515
* @author D瓜哥 · https://www.diguage.com
16-
* @since 2020-02-07 22:31
16+
* @since 2024-06-20 18:48
1717
*/
1818
public class _0113_PathSumII_2 {
1919
// tag::answer[]
2020

2121
/**
2222
* 原始解法
23+
*
24+
* @author D瓜哥 · https://www.diguage.com
25+
* @since 2024-06-20 18:48
2326
*/
2427
public List<List<Integer>> pathSum(TreeNode root, int sum) {
2528
if (root == null) {
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.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
9+
import static com.diguage.util.TreeNodes.buildTree;
10+
import static java.util.Arrays.asList;
11+
12+
public class _0113_PathSumII_3 {
13+
// tag::answer[]
14+
15+
/**
16+
* @author D瓜哥 · https://www.diguage.com
17+
* @since 2024-06-20 18:48
18+
*/
19+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
20+
List<List<Integer>> result = new LinkedList<>();
21+
List<Integer> path = new ArrayList<>();
22+
backtrack(root, targetSum, result, path);
23+
return result;
24+
}
25+
26+
private void backtrack(TreeNode root, int targetSum,
27+
List<List<Integer>> result, List<Integer> path) {
28+
if (root == null) {
29+
return;
30+
}
31+
int nextSum = targetSum - root.val;
32+
path.add(root.val);
33+
if (nextSum == 0 && root.left == null && root.right == null) {
34+
result.add(new ArrayList(path));
35+
}
36+
backtrack(root.left, nextSum, result, path);
37+
backtrack(root.right, nextSum, result, path);
38+
path.remove(path.size() - 1);
39+
}
40+
// end::answer[]
41+
42+
public static void main(String[] args) {
43+
_0113_PathSumII_3 solution = new _0113_PathSumII_3();
44+
List<List<Integer>> r1 = solution.pathSum(buildTree(asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, null, 5, 1)), 22);
45+
System.out.println(r1);
46+
}
47+
}

0 commit comments

Comments
 (0)