Vertical width of Binary tree | Set 2 | A binary tree node has data , pointer to left child and a pointer to right child ; Function to fill hd in set . ; Third parameter is horizontal distance ; Driver Code ; Creating the above tree
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def fillSet ( root , s , hd ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT fillSet ( root . left , s , hd - 1 ) NEW_LINE s . add ( hd ) NEW_LINE fillSet ( root . right , s , hd + 1 ) NEW_LINE DEDENT def verticalWidth ( root ) : NEW_LINE INDENT s = set ( ) NEW_LINE fillSet ( root , s , 0 ) NEW_LINE return len ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . right . left . right = Node ( 8 ) NEW_LINE root . right . right . right = Node ( 9 ) NEW_LINE print ( verticalWidth ( root ) ) NEW_LINE DEDENT