Skip to content

Commit 5e2e374

Browse files
committed
feat: add solutions to lc problem: No.1483
No.1483.Kth Ancestor of a Tree Node
1 parent d8b21c0 commit 5e2e374

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

solution/1400-1499/1483.Kth Ancestor of a Tree Node/README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ treeAncestor.getKthAncestor(6, 3); // 返回 -1 因为不存在满足要求的
5757

5858
**方法一:动态规划 + 倍增**
5959

60-
题目要我们寻找节点 `node` 的第 $k$ 个祖先节点,如果暴力求解,需要从 `node` 开始向上遍历 $k$ 次,时间复杂度为 $O(k)$,显然会超时。
60+
题目要我们寻找节点 $node$ 的第 $k$ 个祖先节点,如果暴力求解,需要从 $node$ 开始向上遍历 $k$ 次,时间复杂度为 $O(k)$,显然会超时。
6161

62-
我们可以使用动态规划的思想,结合倍增的思想来处理。
62+
我们可以使用动态规划,结合倍增的思想来处理。
6363

6464
我们定义 $p[i][j]$ 表示节点 $i$ 的第 $2^j$ 个祖先节点,即 $p[i][j]$ 表示节点 $i$ 向上走 $2^j$ 步的节点。那么我们可以得到状态转移方程:
6565

@@ -71,7 +71,7 @@ $$
7171

7272
之后对于每次查询,我们可以把 $k$ 拆成二进制的表示形式,然后根据二进制中 $1$ 的位置,累计向上查询,最终得到节点 $node$ 的第 $k$ 个祖先节点。
7373

74-
时间复杂度:初始化为 $O(n \times \log n)$,查询为 $O(\log n)$。空间复杂度$O(n \times \log n)$。其中 $n$ 为树的节点数。
74+
时间复杂度方面,初始化为 $O(n \times \log n)$,查询为 $O(\log n)$。空间复杂度 $O(n \times \log n)$。其中 $n$ 为树的节点数。
7575

7676
<!-- tabs:start -->
7777

@@ -240,6 +240,48 @@ func (this *TreeAncestor) GetKthAncestor(node int, k int) int {
240240
*/
241241
```
242242

243+
### **TypeScript**
244+
245+
```ts
246+
class TreeAncestor {
247+
private p: number[][];
248+
249+
constructor(n: number, parent: number[]) {
250+
const p = new Array(n).fill(0).map(() => new Array(18).fill(-1));
251+
for (let i = 0; i < n; ++i) {
252+
p[i][0] = parent[i];
253+
}
254+
for (let i = 0; i < n; ++i) {
255+
for (let j = 1; j < 18; ++j) {
256+
if (p[i][j - 1] === -1) {
257+
continue;
258+
}
259+
p[i][j] = p[p[i][j - 1]][j - 1];
260+
}
261+
}
262+
this.p = p;
263+
}
264+
265+
getKthAncestor(node: number, k: number): number {
266+
for (let i = 17; i >= 0; --i) {
267+
if (((k >> i) & 1) === 1) {
268+
node = this.p[node][i];
269+
if (node === -1) {
270+
break;
271+
}
272+
}
273+
}
274+
return node;
275+
}
276+
}
277+
278+
/**
279+
* Your TreeAncestor object will be instantiated and called as such:
280+
* var obj = new TreeAncestor(n, parent)
281+
* var param_1 = obj.getKthAncestor(node,k)
282+
*/
283+
```
284+
243285
### **...**
244286

245287
```

solution/1400-1499/1483.Kth Ancestor of a Tree Node/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,48 @@ func (this *TreeAncestor) GetKthAncestor(node int, k int) int {
208208
*/
209209
```
210210

211+
### **TypeScript**
212+
213+
```ts
214+
class TreeAncestor {
215+
private p: number[][];
216+
217+
constructor(n: number, parent: number[]) {
218+
const p = new Array(n).fill(0).map(() => new Array(18).fill(-1));
219+
for (let i = 0; i < n; ++i) {
220+
p[i][0] = parent[i];
221+
}
222+
for (let i = 0; i < n; ++i) {
223+
for (let j = 1; j < 18; ++j) {
224+
if (p[i][j - 1] === -1) {
225+
continue;
226+
}
227+
p[i][j] = p[p[i][j - 1]][j - 1];
228+
}
229+
}
230+
this.p = p;
231+
}
232+
233+
getKthAncestor(node: number, k: number): number {
234+
for (let i = 17; i >= 0; --i) {
235+
if (((k >> i) & 1) === 1) {
236+
node = this.p[node][i];
237+
if (node === -1) {
238+
break;
239+
}
240+
}
241+
}
242+
return node;
243+
}
244+
}
245+
246+
/**
247+
* Your TreeAncestor object will be instantiated and called as such:
248+
* var obj = new TreeAncestor(n, parent)
249+
* var param_1 = obj.getKthAncestor(node,k)
250+
*/
251+
```
252+
211253
### **...**
212254

213255
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class TreeAncestor {
2+
private p: number[][];
3+
4+
constructor(n: number, parent: number[]) {
5+
const p = new Array(n).fill(0).map(() => new Array(18).fill(-1));
6+
for (let i = 0; i < n; ++i) {
7+
p[i][0] = parent[i];
8+
}
9+
for (let i = 0; i < n; ++i) {
10+
for (let j = 1; j < 18; ++j) {
11+
if (p[i][j - 1] === -1) {
12+
continue;
13+
}
14+
p[i][j] = p[p[i][j - 1]][j - 1];
15+
}
16+
}
17+
this.p = p;
18+
}
19+
20+
getKthAncestor(node: number, k: number): number {
21+
for (let i = 17; i >= 0; --i) {
22+
if (((k >> i) & 1) === 1) {
23+
node = this.p[node][i];
24+
if (node === -1) {
25+
break;
26+
}
27+
}
28+
}
29+
return node;
30+
}
31+
}
32+
33+
/**
34+
* Your TreeAncestor object will be instantiated and called as such:
35+
* var obj = new TreeAncestor(n, parent)
36+
* var param_1 = obj.getKthAncestor(node,k)
37+
*/

0 commit comments

Comments
 (0)