-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSmallest Missing Genetic Value in Each Subtree.java
54 lines (47 loc) · 1.56 KB
/
Smallest Missing Genetic Value in Each Subtree.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
class Solution {
public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {
int n = parents.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = 1;
}
int oneIndex = -1;
for (int i = 0; i < n; i++) {
if (nums[i] == 1) {
oneIndex = i;
break;
}
}
// 1 not found
if (oneIndex == -1) {
return res;
}
Map<Integer, Set<Integer>> graph = new HashMap<>();
for (int i = 1; i < n; i++) {
Set<Integer> children = graph.getOrDefault(parents[i], new HashSet<Integer>());
children.add(i);
graph.put(parents[i], children);
}
Set<Integer> visited = new HashSet<Integer>();
int parentIter = oneIndex;
int miss = 1;
while (parentIter >= 0) {
dfs(parentIter, graph, visited, nums);
while (visited.contains(miss)) {
miss++;
}
res[parentIter] = miss;
parentIter = parents[parentIter];
}
return res;
}
public void dfs(int ind, Map<Integer, Set<Integer>> graph, Set<Integer> visited, int []nums) {
if (!visited.contains(nums[ind])) {
Set <Integer> children = graph.getOrDefault(ind, new HashSet<Integer>());
for (int p : children) {
dfs(p, graph, visited, nums);
}
visited.add(nums[ind]);
}
}
}