• + 0 comments
    def topView(root):
    Q=deque([])
    top_view={}
    
    Q.append((root,0))
    while Q:
        node,h = Q.popleft()
        if not node:
            continue
        if h not in top_view:
            top_view[h]=node
        Q.append((node.left,h-1))
        Q.append((node.right,h+1))
    

    for some reason they don't let me post the bottom half, anyway sort top_view by key and print the values