forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
50 lines (48 loc) · 1.16 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
class Solution {
private List<Integer>[] g;
private boolean[] vis;
private boolean[] has;
private int[] nums;
public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {
int n = nums.length;
this.nums = nums;
g = new List[n];
vis = new boolean[n];
has = new boolean[n + 2];
Arrays.setAll(g, i -> new ArrayList<>());
int idx = -1;
for (int i = 0; i < n; ++i) {
if (i > 0) {
g[parents[i]].add(i);
}
if (nums[i] == 1) {
idx = i;
}
}
int[] ans = new int[n];
Arrays.fill(ans, 1);
if (idx == -1) {
return ans;
}
for (int i = 2; idx != -1; idx = parents[idx]) {
dfs(idx);
while (has[i]) {
++i;
}
ans[idx] = i;
}
return ans;
}
private void dfs(int i) {
if (vis[i]) {
return;
}
vis[i] = true;
if (nums[i] < has.length) {
has[nums[i]] = true;
}
for (int j : g[i]) {
dfs(j);
}
}
}