-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathKth Ancestor of a Tree Node.js
57 lines (47 loc) · 1.1 KB
/
Kth Ancestor of a Tree Node.js
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
// Runtime: 354 ms (Top 80.0%) | Memory: 113.40 MB (Top 40.0%)
/**
* @param {number} n
* @param {number[]} parent
*/
let par;
var TreeAncestor = function(n, parent) {
let p = new Array(n);
for(let i=0;i<n;i++){
p[i] = new Array(20);
}
for(let i=0;i<n;i++){
p[i][0] = parent[i];
}
for(let j=1;j<20;j++){
for(let i=0;i<n;i++){
if(p[i][j-1]==-1){
p[i][j]= -1;
}
else{
p[i][j] = p[p[i][j-1]][j-1];
}
}
}
par = p;
}
/**
* @param {number} node
* @param {number} k
* @return {number}
*/
TreeAncestor.prototype.getKthAncestor = function(node, k) {
for(let i=0;i<20;i++){
if((k>>i)&1){
node = par[node][i];
if(node==-1){
return -1;
}
}
}
return node;
};
/**
* Your TreeAncestor object will be instantiated and called as such:
* var obj = new TreeAncestor(n, parent)
* var param_1 = obj.getKthAncestor(node,k)
*/