Skip to content

Commit 2f125e9

Browse files
author
liwentian
committed
fd
1 parent 6b9e369 commit 2f125e9

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

ebook/tree/Tree.tex

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,16 +1331,11 @@ \subsubsection{Solution I}
13311331
}
13321332
if (root.val == sum) {
13331333
count[0]++;
1334-
// return;
1334+
// 这里不用返回,因为下面的路径和可能为0;
13351335
}
13361336

1337-
if (root.left != null) {
1338-
helper(root.left, sum - root.val, count);
1339-
}
1340-
1341-
if (root.right != null) {
1342-
helper(root.right, sum - root.val, count);
1343-
}
1337+
helper(root.left, sum - root.val, count);
1338+
helper(root.right, sum - root.val, count);
13441339
}
13451340
\end{Code}
13461341

solution/src/main/java/com/inuker/solution/PathSumIII.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public int pathSum(TreeNode root, int sum) {
2727
}
2828

2929
/**
30-
* 既可以算上,也可以不算上root
30+
* 既可以算上,也可以不算上root
3131
*/
3232
private void helperSum(TreeNode root, int sum, int[] count) {
3333
if (root == null) {
@@ -51,16 +51,11 @@ private void helper(TreeNode root, int sum, int[] count) {
5151
}
5252
if (root.val == sum) {
5353
count[0]++;
54-
// return;
54+
// 这里不用返回,因为下面的路径和可能为0;
5555
}
5656

57-
if (root.left != null) {
58-
helper(root.left, sum - root.val, count);
59-
}
60-
61-
if (root.right != null) {
62-
helper(root.right, sum - root.val, count);
63-
}
57+
helper(root.left, sum - root.val, count);
58+
helper(root.right, sum - root.val, count);
6459
}
6560

6661
/* 如果要给路径打出来

0 commit comments

Comments
 (0)