-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumIII.java
More file actions
58 lines (42 loc) · 1.61 KB
/
Copy pathPathSumIII.java
File metadata and controls
58 lines (42 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package LeetcodePractice;
public class PathSumIII {
public static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public int pathSum(TreeNode root, int targetSum) {
if (root == null) return 0;
int count = countPathsFromNode(root, targetSum);
count += pathSum(root.left, targetSum);
count += pathSum(root.right, targetSum);
return count;
}
private int countPathsFromNode(TreeNode node, int sum) {
if (node == null) return 0;
int count = 0;
if (node.val == sum) count++;
count += countPathsFromNode(node.left, sum - node.val);
count += countPathsFromNode(node.right, sum - node.val);
return count;
}
public static TreeNode buildExampleTree1() {
TreeNode node3 = new TreeNode(3, new TreeNode(3), new TreeNode(-2));
TreeNode node2 = new TreeNode(2, null, new TreeNode(1));
TreeNode node5 = new TreeNode(5, node3, node2);
TreeNode node_3 = new TreeNode(-3, null, new TreeNode(11));
return new TreeNode(10, node5, node_3);
}
public static void main(String[] args) {
PathSumIII solution = new PathSumIII();
TreeNode root = buildExampleTree1();
int targetSum = 8;
int result = solution.pathSum(root, targetSum);
System.out.println("Number of paths with sum " + targetSum + ": " + result); // Expected: 3
}
}