• + 0 comments

    from collections import deque def topView(root): if(root==None): return 0 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))
    print(" ".join(str(hd_map[hd]) for hd in sorted(hd_map)))