forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkth-ancestor-of-a-tree-node.py
43 lines (40 loc) · 1.2 KB
/
kth-ancestor-of-a-tree-node.py
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
# Time: ctor: O(n * logh)
# get: O(logh)
# Space: O(n * logh)
# binary jump solution (frequently used in competitive programming)
# Template:
# https://github.com/kamyu104/FacebookHackerCup-2019/blob/master/Final%20Round/little_boat_on_the_sea.py
class TreeAncestor(object):
def __init__(self, n, parent):
"""
:type n: int
:type parent: List[int]
"""
par = [[p] if p != -1 else [] for p in parent]
q = [par[i] for i, p in enumerate(parent) if p != -1]
i = 0
while q:
new_q = []
for p in q:
if not (i < len(par[p[i]])):
continue
p.append(par[p[i]][i])
new_q.append(p)
q = new_q
i += 1
self.__parent = par
def getKthAncestor(self, node, k):
"""
:type node: int
:type k: int
:rtype: int
"""
par, i, pow_i_of_2 = self.__parent, 0, 1
while pow_i_of_2 <= k:
if k & pow_i_of_2:
if not (i < len(par[node])):
return -1
node = par[node][i]
i += 1
pow_i_of_2 *= 2
return node