We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First i was using recursive method and i was facing the same problem of run time error then after reading your comment i changed it to iterative method but still i am getting "terminated due to timeout" . Can you please help
def dfs(v,visited,tree,v1,lis):
visited[v]=True
total=tree[v]
newlis=[]
for x in lis[v]:
if x!=v1:
newlis.append(x)
total=total+tree[x]
while newlis:
y=newlis.pop()
visited[y]=True
for z in lis[y]:
if visited[z]==False and z!=v1:
visited[z]=True
newlis.append(z)
total=total+tree[z]
return total
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cut the Tree
You are viewing a single comment's thread. Return to all comments →
First i was using recursive method and i was facing the same problem of run time error then after reading your comment i changed it to iterative method but still i am getting "terminated due to timeout" . Can you please help
def dfs(v,visited,tree,v1,lis):