chirkut maulana salution

```java //[]
class Solution {
    void dfs(int v, int p, TreeSet<Integer> po, int[] vl, int[] vis, ArrayList<Integer>[] g) {
        if (vis[v] == 1) return; vis[v] = 1;

        var n = vl.length; if (vl[v] <= n) po.remove(vl[v]);

        for (var c : g[v]) {
            if (c == p || vis[c] == 1) continue;
            dfs(c, v, po, vl, vis, g);
        }
    }

    public int[] smallestMissingValueSubtree(int[] p, int[] vl) {
        var n = p.length;

        var po = new TreeSet<Integer>(); for (var i = 1; i <= n + 1; i++) po.add(i);

        var g = (ArrayList<Integer>[]) new ArrayList[n]; for (var i = 0; i < n; i++) g[i] = new ArrayList<>(); for (var i = 1; i < p.length; i++) { g[p[i]].add(i); g[i].add(p[i]); }

        var an = new int[n]; Arrays.fill(an, 1);

        var v1 = -1; for (var i = 0; i < n; i++) if (vl[i] == 1) v1 = i; if (v1 == -1) return an;

        var vis = new int[n]; Arrays.fill(vis, 0);

        while (v1 != -1) {
            dfs(v1, p[v1], po, vl, vis, g);
            an[v1] = po.iterator().next();
            v1 = p[v1];
        }

        return an;
    }
}

```
['Java']