We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c78e35a commit 586eedcCopy full SHA for 586eedc
week10/Leetcode_(Path Sum III).java
@@ -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