• + 0 comments

    def tree_Coords(root, d, radius, level): if not root: return if level not in d.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)

    def topView(root): radius = 0 level = 0 if not root: return d = 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).
    for level in d.keys():
        for radius in d[level].keys():
            if radius not in res.keys():
                res[radius] = d[level][radius]
    
    for r in sorted(res.keys()):
        print(res[r], end=" ")