Skip to content

Commit 0608fad

Browse files
add 2385
1 parent 1e6b643 commit 0608fad

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2404.java) || Easy ||
3636
| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2399.java) || Medium ||
3737
| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2395.java) || Easy ||
38+
| 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2380.java) || Medium ||BFS
3839
| 2380 | [Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2380.java) || Medium ||
3940
| 2379 | [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2379.java) || Easy ||
4041
| 2373 | [Largest Local Values in a Matrix](https://leetcode.com/problems/largest-local-values-in-a-matrix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_2373.java) || Easy ||

Diff for: src/main/java/com/fishercoder/solutions/_2385.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.*;
6+
7+
public class _2385 {
8+
public static class Solution1 {
9+
public int amountOfTime(TreeNode root, int start) {
10+
Map<Integer, List<Integer>> adjList = new HashMap<>();
11+
buildAdjList(root, adjList);
12+
Queue<Integer> q = new LinkedList<>();
13+
q.offer(start);
14+
Set<Integer> visited = new HashSet<>();
15+
visited.add(start);
16+
int times = -1;
17+
while (!q.isEmpty()) {
18+
int size = q.size();
19+
for (int i = 0; i < size; i++) {
20+
Integer curr = q.poll();
21+
if (adjList.containsKey(curr)) {
22+
for (int node : adjList.get(curr)) {
23+
if (visited.add(node)) {
24+
q.offer(node);
25+
}
26+
}
27+
}
28+
}
29+
times++;
30+
}
31+
return times;
32+
}
33+
34+
private void buildAdjList(TreeNode root, Map<Integer, List<Integer>> adjList) {
35+
if (root == null) {
36+
return;
37+
}
38+
if (root.left != null) {
39+
List<Integer> list = adjList.getOrDefault(root.val, new ArrayList<>());
40+
list.add(root.left.val);
41+
adjList.put(root.val, list);
42+
43+
list = adjList.getOrDefault(root.left.val, new ArrayList<>());
44+
list.add(root.val);
45+
adjList.put(root.left.val, list);
46+
}
47+
48+
if (root.right != null) {
49+
List<Integer> list = adjList.getOrDefault(root.val, new ArrayList<>());
50+
list.add(root.right.val);
51+
adjList.put(root.val, list);
52+
53+
list = adjList.getOrDefault(root.right.val, new ArrayList<>());
54+
list.add(root.val);
55+
adjList.put(root.right.val, list);
56+
}
57+
buildAdjList(root.left, adjList);
58+
buildAdjList(root.right, adjList);
59+
}
60+
}
61+
}

Diff for: src/test/java/com/fishercoder/_2385Test.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.TreeUtils;
4+
import com.fishercoder.solutions._2385;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class _2385Test {
13+
private static _2385.Solution1 solution1;
14+
15+
@BeforeEach
16+
public void setup() {
17+
solution1 = new _2385.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(4, solution1.amountOfTime(TreeUtils.constructBinaryTree(Arrays.asList(1, 5, 3, null, 4, 10, 6, 9, 2)), 3));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(1, solution1.amountOfTime(TreeUtils.constructBinaryTree(Arrays.asList(2, 5)), 5));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)