-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathKth Ancestor of a Tree Node.java
47 lines (43 loc) · 1.28 KB
/
Kth Ancestor of a Tree Node.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
// Runtime: 96 ms (Top 79.04%) | Memory: 112.4 MB (Top 41.18%)
class TreeAncestor {
int n;
int[] parent;
List<Integer>[] nodeInPath;
int[] nodeIdxInPath;
public TreeAncestor(int n, int[] parent) {
this.n = n;
this.parent = parent;
nodeInPath = new ArrayList[n];
nodeIdxInPath = new int[n];
fill();
}
private void fill() {
boolean[] inner = new boolean[n];
for (int i = 1; i < n; i++) {
inner[parent[i]] = true;
}
for (int i = 1; i < n; i++) {
if (inner[i] || nodeInPath[i] != null) {
continue;
}
List<Integer> path = new ArrayList<>();
int k = i;
while (k != -1) {
path.add(k);
k = parent[k];
}
int m = path.size();
for (int j = 0; j < m; j++) {
int node = path.get(j);
if (nodeInPath[node] != null) break;
nodeInPath[node] = path;
nodeIdxInPath[node] = j;
}
}
}
public int getKthAncestor(int node, int k) {
List<Integer> path = nodeInPath[node];
int idx = nodeIdxInPath[node] + k;
return idx >= path.size() ? -1 : path.get(idx);
}
}