File tree 3 files changed +141
-0
lines changed
solution/1400-1499/1483.Kth Ancestor of a Tree Node
3 files changed +141
-0
lines changed Original file line number Diff line number Diff line change @@ -271,6 +271,54 @@ class TreeAncestor {
271
271
*/
272
272
```
273
273
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
+
274
322
<!-- tabs:end -->
275
323
276
324
<!-- end -->
Original file line number Diff line number Diff line change @@ -263,6 +263,54 @@ class TreeAncestor {
263
263
*/
264
264
```
265
265
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
+
266
314
<!-- tabs:end -->
267
315
268
316
<!-- end -->
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments