Question

Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1 is the root K
Python 1 Task 2 A node is said to be at level X if the length of the shortest path between that node and root is X - 1. So, t
For example, given array A such that: A[2] = e A[3] 7 A[4]-8 the function should return 2. Explanation: Nodes at level 1: [1]
The sum of associated values of nodes at level 2 equals 7, and this is the maximum sum among all levels. Assume that: . N is
Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1 is the root Kand 2 of the tree and each node has an integer value associated with it. Such a tree may be represented as an array of N integers by writing down values from consecutive nodes For example, the tree below 8 Test might be represented as an array o A node is said to be at level X if the length of the shortest path 2
Python 1 Task 2 A node is said to be at level X if the length of the shortest path between that node and root is X - 1. So, the root is at level 1, the children of the root are at level 2, and so on. Your task is to find the smallest level number X such that the sum of all the nodes at level X is maximal. Write a function: def solution(A) that, given an array A consisting of N integers denoting a tree of N vertices, returns the level with the maximal sum of values of its vertices. If there is more than one of such levels, your function should return the smallest level. Note that A[K denotes value associated with node K+1
For example, given array A such that: A[2] = e A[3] 7 A[4]-8 the function should return 2. Explanation: Nodes at level 1: [1], sum equals-1 Nodes at level 2: [2, 3], sum equals 7 +0 Nodes at level 3: [4, 51, sum equals 7+ The sum of associated values of nodes at level 2 equals 7, and this is the maximum sum among all levels. Test O Assume that: . N is an integer within the range [1..1,000] . each element of 2
The sum of associated values of nodes at level 2 equals 7, and this is the maximum sum among all levels. Assume that: . N is an integer within the range [1..1,000]; . each element of array A is an integer within the range I-100,000..100,000]. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPLANATION IN CODE COMMENTS:

import queue
q = queue.Queue(20)

def solution(A):
#q to keep track of levels and nodes at that level
#First push the root node level
q.put(1)
#s to track the global maximum sum so far
s = -10000
#lvl to track the global maximum sum level
lvl = 0
#to iterate all levels
k=1
#exit condition variable for loop
flag = True
while flag:
#lsum keeps track of sum at each level
lsum = 0
#Gets no. of nodes at current level
siz = q.qsize()
#loop at all nodes at current level
while siz>0:
v = q.get()
#if that node does not exist in the array break the loop
if v > len(A):
#all nodes are traversed , exit all loops
flag = False
break
#add it to lsum
lsum+=A[v-1]
#push its child nodes into queue
q.put(2*v)
q.put(2*v+1)
siz-=1
#if globalSum < localSum then update globalSum and lvl of that sum
if s<lsum:
s=lsum
lvl = k
#increase the level
k+=1
#return level
return lvl
  
a = [-1,7,0,7,-8]
print(solution(a))

ktuzzy Spyder (Python 3.J) Eile Edit Searh So un Debug Consales Projects Tools View Help s Varisble explorer roject explorerktuzzy Spyder (Python 3.J) Eile Edit Searh So un Debug Consales Projects Tools View Help gig Venable explorer roject explorerIn [32]: runfile(C:/Users/Rogue/Desktop/sumLevelB.py, wdir-C:/Users/Rogue/ Desktop)

Add a comment
Know the answer?
Add Answer to:
Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT