Skip to content

Commit 586eedc

Browse files
committed
Leetcode : Path Sum III
1 parent c78e35a commit 586eedc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

week10/Leetcode_(Path Sum III).java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
private int count;
3+
private int targetSum;
4+
private long[] sum;
5+
6+
public int pathSum(TreeNode root, int _targetSum) {
7+
targetSum = _targetSum;
8+
sum = new long[1001];
9+
dfs(root, 1);
10+
return count;
11+
}
12+
13+
public void dfs(TreeNode node, int depth) {
14+
if(node == null) {
15+
return;
16+
}
17+
18+
sum[depth] = sum[depth-1] + node.val;
19+
for(int i=depth-1; i>=0; i--) {
20+
if(sum[depth] - sum[i] == targetSum) {
21+
count++;
22+
}
23+
}
24+
25+
dfs(node.left, depth+1);
26+
dfs(node.right, depth+1);
27+
}
28+
}

0 commit comments

Comments
 (0)