-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1483-kth-ancestor-of-a-tree-node.js
53 lines (49 loc) · 1.54 KB
/
1483-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
/**
* 1483. Kth Ancestor of a Tree Node
* https://leetcode.com/problems/kth-ancestor-of-a-tree-node/
* Difficulty: Hard
*
* You are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array
* parent where parent[i] is the parent of ith node. The root of the tree is node 0. Find the
* kth ancestor of a given node.
*
* The kth ancestor of a tree node is the kth node in the path from that node to the root node.
*
* Implement the TreeAncestor class:
* - TreeAncestor(int n, int[] parent) Initializes the object with the number of nodes in the tree
* and the parent array.
* - int getKthAncestor(int node, int k) return the kth ancestor of the given node node. If there
* is no such ancestor, return -1.
*/
/**
* @param {number} n
* @param {number[]} parent
*/
var TreeAncestor = function(n, parent) {
const maxDepth = Math.ceil(Math.log2(n));
this.ancestors = Array.from({ length: n }, () => new Array(maxDepth + 1).fill(-1));
for (let i = 0; i < n; i++) {
this.ancestors[i][0] = parent[i];
}
for (let j = 1; j <= maxDepth; j++) {
for (let i = 0; i < n; i++) {
if (this.ancestors[i][j - 1] !== -1) {
this.ancestors[i][j] = this.ancestors[this.ancestors[i][j - 1]][j - 1];
}
}
}
};
/**
* @param {number} node
* @param {number} k
* @return {number}
*/
TreeAncestor.prototype.getKthAncestor = function(node, k) {
let current = node;
for (let j = 0; k > 0 && current !== -1; j++, k >>= 1) {
if (k & 1) {
current = this.ancestors[current][j];
}
}
return current;
};