Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def answer(h, q):
- #Setting up Variables to use later on
- size = (2**h)-1
- tree = []
- vals = []
- final = []
- #Populate Values to be inserted into tree
- #Fill Tree with filler
- for i in range(0,size,1):
- vals.append(i+1)
- tree.append(-2)
- #Function used to insert values to tree
- def addVal(i):
- tree[i] = vals.pop(0)
- #PostOrder Insertion
- def postOrder(i):
- if (i*2)+1 > size or (i+1)*2 > size:
- addVal(i)
- else:
- postOrder((i*2)+1)
- postOrder((i+1)*2)
- addVal(i)
- #returns parent node of a given index
- def getParent(i):
- if i == 0 or i > size:
- return -1
- else:
- return (i-1)/2
- #returns value being stored at indexed node
- def getValue(i):
- if i == -1:
- return -1
- else:
- return tree[i]
- #Populate Tree with Values
- postOrder(0)
- #return List composed of the values of the parents of provided list
- for i in range(0, len(q), 1):
- final.append(getValue(getParent(tree.index(q.pop(0)))))
- #return List with Values of Parents
- return final
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement