• + 0 comments
    def topView(root):
        if not root:
            return
        
        hd_map = {}
        
        queue = deque([(root, 0)])
        
        while queue:
            node, hd = queue.popleft()
            if hd not in hd_map:
                hd_map[hd] = node.info
                
            if node.left:
                queue.append((node.left, hd - 1))
            if node.right:
                queue.append((node.right, hd + 1))
        for hd in sorted(hd_map.keys()):
            print(hd_map[hd], end=" ")