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.
I try to find a better definition of top view since the one presented is terrible: the list nodes, from left to the right, that projected vertically from the top to the bottom, are NOT covered by other nodes.
# layer: meant as horizontal layer, integer# < 0 (left of the root) or > 0 (right of the root)# depth: depth in the three from zero, always positivedefexplore(node,results,layer:int=0,depth:int=0):iflayernotinresults:# if it a new layer, no matter of the current depthresults[layer]={'depth':depth,'content':node.info}else:# there was already a node on this layerifdepth<results[layer].get('depth'):results[layer]={'depth':depth,'content':node.info}ifnode.leftisnotNone:explore(node.left,results,layer-1,depth+1)ifnode.rightisnotNone:explore(node.right,results,layer+1,depth+1)deftopView(root):#Write your code hereresults={}explore(root,results)min_index=min(results.keys())max_index=max(results.keys())foriinrange(min_index,max_index+1):print(results[i].get('content'),end=" ")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree : Top View
You are viewing a single comment's thread. Return to all comments →
I try to find a better definition of top view since the one presented is terrible: the list nodes, from left to the right, that projected vertically from the top to the bottom, are NOT covered by other nodes.