-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparentPostOrder.py
More file actions
27 lines (27 loc) · 859 Bytes
/
Copy pathparentPostOrder.py
File metadata and controls
27 lines (27 loc) · 859 Bytes
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
"""
Given a tree of height h constructed with integers
[1...(2^h)-1] in postorder traversal, find the integers
who are direct parents of each value in q,
or -1 if the value is the root's.
"""
def answer(h, q):
retVal = []
for converter in q:
currHeight = h
prev = -1
start = 1
end = (2**currHeight)-1
# Find the value in the binary tree. Each level traversal will
# get rid of 2^(currHeight-1) entries.
while converter != end:
prev = end
# Go down right branch
if converter >= start + 2**(currHeight-1) - 1:
start = start + 2**(currHeight-1) - 1
end -= 1
# Go down left branch
else:
end = end - 2**(currHeight-1)
currHeight -= 1
retVal.append(prev)
return retVal