Vertical width of Binary tree | Set 1 | class to create a new tree node ; get vertical width ; traverse left ; if curr is decrease then get value in minimum ; if curr is increase then get value in maximum ; traverse right ; 1 is added to include root in the width ; Driver Code
class newNode : 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 lengthUtil ( root , maximum , minimum , curr = 0 ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT lengthUtil ( root . left , maximum , minimum , curr - 1 ) NEW_LINE if ( minimum [ 0 ] > curr ) : NEW_LINE INDENT minimum [ 0 ] = curr NEW_LINE DEDENT if ( maximum [ 0 ] < curr ) : NEW_LINE INDENT maximum [ 0 ] = curr NEW_LINE DEDENT lengthUtil ( root . right , maximum , minimum , curr + 1 ) NEW_LINE DEDENT def getLength ( root ) : NEW_LINE INDENT maximum = [ 0 ] NEW_LINE minimum = [ 0 ] NEW_LINE lengthUtil ( root , maximum , minimum , 0 ) NEW_LINE return ( abs ( minimum [ 0 ] ) + maximum [ 0 ] ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 7 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( getLength ( root ) ) NEW_LINE DEDENT