• + 0 comments
    from collections import deque
        
    
    def topView(root):
        
        result = {}
        
        bfs = deque()
        bfs.append((root, 0))
        
        while bfs:
            node, distance = bfs.popleft()
            
            if distance not in result:
                result[distance] = node.info
                
            if node.left:
                bfs.append((node.left, distance - 1))
            
            if node.right:
                bfs.append((node.right, distance + 1))
            
        result = dict(sorted(result.items()))
        print(" ".join(map(str, result.values())))