Skip to content

Commit 74cdddd

Browse files
committed
Add solution #1483
1 parent 2fcf127 commit 74cdddd

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,324 LeetCode solutions in JavaScript
1+
# 1,325 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1134,6 +1134,7 @@
11341134
1480|[Running Sum of 1d Array](./solutions/1480-running-sum-of-1d-array.js)|Easy|
11351135
1481|[Least Number of Unique Integers after K Removals](./solutions/1481-least-number-of-unique-integers-after-k-removals.js)|Medium|
11361136
1482|[Minimum Number of Days to Make m Bouquets](./solutions/1482-minimum-number-of-days-to-make-m-bouquets.js)|Medium|
1137+
1483|[Kth Ancestor of a Tree Node](./solutions/1483-kth-ancestor-of-a-tree-node.js)|Hard|
11371138
1486|[XOR Operation in an Array](./solutions/1486-xor-operation-in-an-array.js)|Easy|
11381139
1491|[Average Salary Excluding the Minimum and Maximum Salary](./solutions/1491-average-salary-excluding-the-minimum-and-maximum-salary.js)|Easy|
11391140
1492|[The kth Factor of n](./solutions/1492-the-kth-factor-of-n.js)|Medium|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* 1483. Kth Ancestor of a Tree Node
3+
* https://leetcode.com/problems/kth-ancestor-of-a-tree-node/
4+
* Difficulty: Hard
5+
*
6+
* You are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array
7+
* parent where parent[i] is the parent of ith node. The root of the tree is node 0. Find the
8+
* kth ancestor of a given node.
9+
*
10+
* The kth ancestor of a tree node is the kth node in the path from that node to the root node.
11+
*
12+
* Implement the TreeAncestor class:
13+
* - TreeAncestor(int n, int[] parent) Initializes the object with the number of nodes in the tree
14+
* and the parent array.
15+
* - int getKthAncestor(int node, int k) return the kth ancestor of the given node node. If there
16+
* is no such ancestor, return -1.
17+
*/
18+
19+
/**
20+
* @param {number} n
21+
* @param {number[]} parent
22+
*/
23+
var TreeAncestor = function(n, parent) {
24+
const maxDepth = Math.ceil(Math.log2(n));
25+
this.ancestors = Array.from({ length: n }, () => new Array(maxDepth + 1).fill(-1));
26+
27+
for (let i = 0; i < n; i++) {
28+
this.ancestors[i][0] = parent[i];
29+
}
30+
31+
for (let j = 1; j <= maxDepth; j++) {
32+
for (let i = 0; i < n; i++) {
33+
if (this.ancestors[i][j - 1] !== -1) {
34+
this.ancestors[i][j] = this.ancestors[this.ancestors[i][j - 1]][j - 1];
35+
}
36+
}
37+
}
38+
};
39+
40+
/**
41+
* @param {number} node
42+
* @param {number} k
43+
* @return {number}
44+
*/
45+
TreeAncestor.prototype.getKthAncestor = function(node, k) {
46+
let current = node;
47+
for (let j = 0; k > 0 && current !== -1; j++, k >>= 1) {
48+
if (k & 1) {
49+
current = this.ancestors[current][j];
50+
}
51+
}
52+
return current;
53+
};

0 commit comments

Comments
 (0)