Skip to content

Commit 5d22de2

Browse files
committed
finish "113. Path Sum II". Fix #113
1 parent 549550f commit 5d22de2

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

README.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,11 @@
682682
|{doc_base_url}/0112-path-sum.adoc[Note]
683683
|Easy
684684

685-
//|113
686-
//|{leetcode_base_url}/path-sum-ii/[Path Sum II]
687-
//|{source_base_url}/_0113_PathSumII.java[Java]
688-
//|{doc_base_url}/0113-path-sum-ii.adoc[Note]
689-
//|Medium
685+
|113
686+
|{leetcode_base_url}/path-sum-ii/[Path Sum II]
687+
|{source_base_url}/_0113_PathSumII.java[Java]
688+
|{doc_base_url}/0113-path-sum-ii.adoc[Note]
689+
|Medium
690690

691691
|114
692692
|{leetcode_base_url}/flatten-binary-tree-to-linked-list/[Flatten Binary Tree to Linked List]

docs/0113-path-sum-ii.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ Return:
3131
]
3232
----
3333

34+
=== 解题分析
35+
36+
利用回溯+深度优先搜索即可解决。
37+
38+
这样要重点注意的是:回溯时,前进和后退要成对出现。
39+
40+
=== 参考资料
41+
42+
. https://leetcode-cn.com/problems/path-sum-ii/solution/dfs-by-powcai-2/[DFS - 路径总和 II - 力扣(LeetCode)]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.TreeNode;
4+
5+
import java.util.*;
6+
7+
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
8+
import static java.util.Arrays.asList;
9+
10+
/**
11+
* = 113. Path Sum II
12+
*
13+
* https://leetcode.com/problems/path-sum-ii/[Path Sum II - LeetCode]
14+
*
15+
* @author D瓜哥, https://www.diguage.com/
16+
* @since 2020-02-07 22:31
17+
*/
18+
public class _0113_PathSumII {
19+
20+
/**
21+
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Path Sum II.
22+
* Memory Usage: 41.8 MB, less than 6.06% of Java online submissions for Path Sum II.
23+
*/
24+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
25+
List<List<Integer>> result = new LinkedList<>();
26+
dfs(root, sum, result, new ArrayDeque<>());
27+
return result;
28+
}
29+
30+
private void dfs(TreeNode root, int sum, List<List<Integer>> result, Deque<Integer> current) {
31+
if (Objects.isNull(root)) {
32+
return;
33+
}
34+
sum -= root.val;
35+
current.addLast(root.val);
36+
if (sum == 0 && Objects.isNull(root.left) && Objects.isNull(root.right)) {
37+
result.add(new ArrayList<>(current));
38+
}
39+
dfs(root.left, sum, result, current);
40+
dfs(root.right, sum, result, current);
41+
current.removeLast();
42+
}
43+
44+
public static void main(String[] args) {
45+
_0113_PathSumII solution = new _0113_PathSumII();
46+
List<List<Integer>> r1 = solution.pathSum(buildTree(asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, null, 5, 1)), 22);
47+
System.out.println(r1);
48+
}
49+
}

0 commit comments

Comments
 (0)