Skip to content

Commit 106b357

Browse files
authored
Added task 437.
1 parent 31cf869 commit 106b357

File tree

5 files changed

+130
-20
lines changed

5 files changed

+130
-20
lines changed

src/main/java/com_github_leetcode/TreeNode.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com_github_leetcode;
22

3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Queue;
6+
37
@SuppressWarnings("java:S1104")
48
public class TreeNode {
59
public int val;
@@ -16,26 +20,24 @@ public TreeNode(int val, TreeNode left, TreeNode right) {
1620
this.right = right;
1721
}
1822

19-
private static int index = 0;
20-
21-
public static TreeNode create(int[] arr) {
22-
index = 0;
23-
return create1(arr);
24-
}
25-
26-
private static TreeNode create1(int[] arr) {
27-
if (arr.length == index) {
28-
return null;
29-
}
30-
if (arr[index] == '#') {
31-
index++;
32-
return null;
33-
} else {
34-
TreeNode res = new TreeNode(arr[index++]);
35-
res.left = create1(arr);
36-
res.right = create1(arr);
37-
return res;
23+
public static TreeNode create(List<Integer> treeValues) {
24+
TreeNode root = new TreeNode(treeValues.get(0));
25+
Queue<TreeNode> queue = new LinkedList<>();
26+
queue.offer(root);
27+
int i = 1;
28+
while (i < treeValues.size()) {
29+
TreeNode curr = queue.poll();
30+
if (treeValues.get(i) != null) {
31+
curr.left = new TreeNode(treeValues.get(i));
32+
queue.offer(curr.left);
33+
}
34+
if (++i < treeValues.size() && treeValues.get(i) != null) {
35+
curr.right = new TreeNode(treeValues.get(i));
36+
queue.offer(curr.right);
37+
}
38+
i++;
3839
}
40+
return root;
3941
}
4042

4143
public String toString() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0401_0500.s0437_path_sum_iii;
2+
3+
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/*
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode() {}
16+
* TreeNode(int val) { this.val = val; }
17+
* TreeNode(int val, TreeNode left, TreeNode right) {
18+
* this.val = val;
19+
* this.left = left;
20+
* this.right = right;
21+
* }
22+
* }
23+
*/
24+
public class Solution {
25+
private int count(TreeNode root, int targetSum, Map<Integer, Integer> hashMap, int currentSum) {
26+
if (root == null) {
27+
return 0;
28+
}
29+
int count = 0;
30+
if (hashMap.containsKey(currentSum + root.val - targetSum)) {
31+
count = count + hashMap.get(currentSum + root.val - targetSum);
32+
}
33+
hashMap.put(currentSum + root.val, hashMap.getOrDefault(currentSum + root.val, 0) + 1);
34+
int l1 = count(root.left, targetSum, hashMap, currentSum + root.val);
35+
int l2 = count(root.right, targetSum, hashMap, currentSum + root.val);
36+
hashMap.put(currentSum + root.val, hashMap.getOrDefault(currentSum + root.val, 0) - 1);
37+
return l1 + l2 + count;
38+
}
39+
40+
public int pathSum(TreeNode root, int targetSum) {
41+
Map<Integer, Integer> map = new HashMap<>();
42+
map.put(0, 1);
43+
return count(root, targetSum, map, 0);
44+
}
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
437\. Path Sum III
2+
3+
Medium
4+
5+
Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`.
6+
7+
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
12+
13+
**Input:** root = \[10,5,-3,3,2,null,11,3,-2,null,1\], targetSum = 8
14+
15+
**Output:** 3
16+
17+
**Explanation:** The paths that sum to 8 are shown.
18+
19+
**Example 2:**
20+
21+
**Input:** root = \[5,4,8,11,null,13,4,7,2,null,null,5,1\], targetSum = 22
22+
23+
**Output:** 3
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range `[0, 1000]`.
28+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
29+
* `-1000 <= targetSum <= 1000`

src/test/java/g0101_0200/s0124_binary_tree_maximum_path_sum/SolutionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import static org.hamcrest.MatcherAssert.assertThat;
55

66
import com_github_leetcode.TreeNode;
7+
import java.util.Arrays;
78
import org.junit.jupiter.api.Test;
89

910
class SolutionTest {
1011
@Test
1112
void findMaxPathSum() {
12-
TreeNode treeNode = TreeNode.create(new int[] {1, 2, 3});
13+
TreeNode treeNode = TreeNode.create(Arrays.asList(1, 2, null, 3));
1314
assertThat(new Solution().findMaxPathSum(treeNode), equalTo(6));
1415
}
1516
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0401_0500.s0437_path_sum_iii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void pathSum() {
13+
assertThat(
14+
new Solution()
15+
.pathSum(
16+
TreeNode.create(
17+
Arrays.asList(10, 5, -3, 3, 2, null, 11, 3, -2, null, 1)),
18+
8),
19+
equalTo(3));
20+
}
21+
22+
@Test
23+
void pathSum2() {
24+
assertThat(
25+
new Solution()
26+
.pathSum(
27+
TreeNode.create(
28+
Arrays.asList(
29+
5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1)),
30+
22),
31+
equalTo(3));
32+
}
33+
}

0 commit comments

Comments
 (0)