-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
69 lines (54 loc) · 1.63 KB
/
Solution.java
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
59
60
61
62
63
64
65
66
67
68
69
//
// Tree, DFS, Hash Table(Minimum Time to Collect All Apples in a Tree)
// https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
// Created by hyungwook on 2022/03/04.
//
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Solution {
List<Boolean> hasApple;
boolean[] isVisit;
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
this.isVisit = new boolean[n];
this.hasApple = hasApple;
HashMap<Integer, Node> map = new HashMap<>();
for (int[] edge : edges) {
int from = edge[0];
int to = edge[1];
Node parent = map.getOrDefault(from, new Node(from));
Node child = map.getOrDefault(to, new Node(to));
parent.children.add(child);
child.children.add(parent);
map.put(from, parent);
map.put(to, child);
}
Node root = map.get(0);
isVisit[0] = true;
return Math.max(0, dfs(root) - 2);
}
private int dfs(Node node) {
if (node == null) {
return 0;
}
int traverseNum = 0;
for (Node child : node.children) {
if (!isVisit[child.n]) {
isVisit[child.n] = true;
traverseNum += dfs(child);
}
}
if (traverseNum == 0 && !hasApple.get(node.n)) {
return 0;
} else {
return traverseNum + 2;
}
}
static class Node {
int n;
List<Node> children = new ArrayList<>();
public Node(int n) {
this.n = n;
}
}
}