Bottom View of a Binary Tree | Tree node class ; Constructor of tree node ; printBottomViewUtil function ; Base case ; If current level is more than or equal to maximum level seen so far for the same horizontal distance or horizontal distance is seen for the first time , update the dictionary ; recur for left subtree by decreasing horizontal distance and increasing level by 1 ; recur for right subtree by increasing horizontal distance and increasing level by 1 ; printBottomView function ; Create a dictionary where key -> relative horizontal distance of the node from root node and value -> pair containing node 's  value and its level ; Traverse the dictionary in sorted order of their keys and print the bottom view ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , key = None , left = None , right = None ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def printBottomViewUtil ( root , d , hd , level ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if hd in d : NEW_LINE INDENT if level >= d [ hd ] [ 1 ] : NEW_LINE INDENT d [ hd ] = [ root . data , level ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT d [ hd ] = [ root . data , level ] NEW_LINE DEDENT printBottomViewUtil ( root . left , d , hd - 1 , level + 1 ) NEW_LINE printBottomViewUtil ( root . right , d , hd + 1 , level + 1 ) NEW_LINE DEDENT def printBottomView ( root ) : NEW_LINE INDENT d = dict ( ) NEW_LINE printBottomViewUtil ( root , d , 0 , 0 ) NEW_LINE for i in sorted ( d . keys ( ) ) : NEW_LINE INDENT print ( d [ i ] [ 0 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 25 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE print ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : " ) NEW_LINE printBottomView ( root ) NEW_LINE DEDENT