• + 0 comments
    python 3                                                                                                                       
    def topView(root):                                                                                             
        queue=[]                                                                                                       
        top_view={}                                                                                                   
        queue.append([root,0])                                                                              
        while(queue):                                                                                               
            node,level=queue.pop(0)  # to remove front element                       
            if level not in top_view:                                                                           
                top_view[level]=node.info                                                                                                                     
            if node.left:
                node.left.level=level-1
                queue.append((node.left, level - 1))
            if node.right:
                node.right.level=level+1
                queue.append((node.right, level + 1))
        print(top_view)
        for level in sorted(top_view):
            print(top_view[level],end=" ")