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.
# Creates a nested dictionary indexed by level and# radius as coordinates pointing to each node.infodeftree_Coords(root,d,radius,level):ifnotroot:returniflevelnotind.keys():d[level]=dict()d[level].update({radius:root.info})tree_Coords(root.left,d,radius-1,level+1)tree_Coords(root.right,d,radius+1,level+1)deftopView(root):radius=0level=0ifnotroot:returnd=dict()tree_Coords(root,d,radius,level)res=dict()# Nested loop scans and adds values to res when no value exists for# a specific radius. If raidus index already exists in res, then there existed# another value with the same radius on a previous level(above).forlevelind.keys():forradiusind[level].keys():ifradiusnotinres.keys():res[radius]=d[level][radius]forrinsorted(res.keys()):print(res[r],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 →