Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e347d8d

Browse files
feat: add cs solution to lc problem: No.1483 (doocs#2540)
No.1483.Kth Ancestor of a Tree Node
1 parent 3337bf3 commit e347d8d

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,54 @@ class TreeAncestor {
271271
*/
272272
```
273273

274+
```cs
275+
public class TreeAncestor {
276+
private int[][] p;
277+
278+
public TreeAncestor(int n, int[] parent) {
279+
p = new int[n][];
280+
for (int i = 0; i < n; i++) {
281+
p[i] = new int[18];
282+
for (int j = 0; j < 18; j++) {
283+
p[i][j] = -1;
284+
}
285+
}
286+
287+
for (int i = 0; i < n; ++i) {
288+
p[i][0] = parent[i];
289+
}
290+
291+
for (int j = 1; j < 18; ++j) {
292+
for (int i = 0; i < n; ++i) {
293+
if (p[i][j - 1] == -1) {
294+
continue;
295+
}
296+
p[i][j] = p[p[i][j - 1]][j - 1];
297+
}
298+
}
299+
}
300+
301+
public int GetKthAncestor(int node, int k) {
302+
for (int i = 17; i >= 0; --i) {
303+
if (((k >> i) & 1) == 1) {
304+
node = p[node][i];
305+
if (node == -1) {
306+
break;
307+
}
308+
}
309+
}
310+
return node;
311+
}
312+
}
313+
314+
315+
/**
316+
* Your TreeAncestor object will be instantiated and called as such:
317+
* TreeAncestor obj = new TreeAncestor(n, parent);
318+
* int param_1 = obj.GetKthAncestor(node,k);
319+
*/
320+
```
321+
274322
<!-- tabs:end -->
275323

276324
<!-- end -->

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,54 @@ class TreeAncestor {
263263
*/
264264
```
265265

266+
```cs
267+
public class TreeAncestor {
268+
private int[][] p;
269+
270+
public TreeAncestor(int n, int[] parent) {
271+
p = new int[n][];
272+
for (int i = 0; i < n; i++) {
273+
p[i] = new int[18];
274+
for (int j = 0; j < 18; j++) {
275+
p[i][j] = -1;
276+
}
277+
}
278+
279+
for (int i = 0; i < n; ++i) {
280+
p[i][0] = parent[i];
281+
}
282+
283+
for (int j = 1; j < 18; ++j) {
284+
for (int i = 0; i < n; ++i) {
285+
if (p[i][j - 1] == -1) {
286+
continue;
287+
}
288+
p[i][j] = p[p[i][j - 1]][j - 1];
289+
}
290+
}
291+
}
292+
293+
public int GetKthAncestor(int node, int k) {
294+
for (int i = 17; i >= 0; --i) {
295+
if (((k >> i) & 1) == 1) {
296+
node = p[node][i];
297+
if (node == -1) {
298+
break;
299+
}
300+
}
301+
}
302+
return node;
303+
}
304+
}
305+
306+
307+
/**
308+
* Your TreeAncestor object will be instantiated and called as such:
309+
* TreeAncestor obj = new TreeAncestor(n, parent);
310+
* int param_1 = obj.GetKthAncestor(node,k);
311+
*/
312+
```
313+
266314
<!-- tabs:end -->
267315

268316
<!-- end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class TreeAncestor {
2+
private int[][] p;
3+
4+
public TreeAncestor(int n, int[] parent) {
5+
p = new int[n][];
6+
for (int i = 0; i < n; i++) {
7+
p[i] = new int[18];
8+
for (int j = 0; j < 18; j++) {
9+
p[i][j] = -1;
10+
}
11+
}
12+
13+
for (int i = 0; i < n; ++i) {
14+
p[i][0] = parent[i];
15+
}
16+
17+
for (int j = 1; j < 18; ++j) {
18+
for (int i = 0; i < n; ++i) {
19+
if (p[i][j - 1] == -1) {
20+
continue;
21+
}
22+
p[i][j] = p[p[i][j - 1]][j - 1];
23+
}
24+
}
25+
}
26+
27+
public int GetKthAncestor(int node, int k) {
28+
for (int i = 17; i >= 0; --i) {
29+
if (((k >> i) & 1) == 1) {
30+
node = p[node][i];
31+
if (node == -1) {
32+
break;
33+
}
34+
}
35+
}
36+
return node;
37+
}
38+
}
39+
40+
41+
/**
42+
* Your TreeAncestor object will be instantiated and called as such:
43+
* TreeAncestor obj = new TreeAncestor(n, parent);
44+
* int param_1 = obj.GetKthAncestor(node,k);
45+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /