Print the longest leaf to leaf path in a Binary tree | Tree node structure used in the program ; Function to find height of a tree ; Update the answer , because diameter of a tree is nothing but maximum value of ( left_height + right_height + 1 ) for each node ; Save the root , this will help us finding the left and the right part of the diameter ; Save the height of left & right subtree as well . ; Prints the root to leaf path ; Print left part of the path in reverse order ; This function finds out all the root to leaf paths ; Append this node to the path array ; If it 's a leaf, so print the  path that led to here ; Print only one path which is equal to the height of the tree . print ( pathlen , " - - - " , maxm ) ; Otherwise try both subtrees ; Computes the diameter of a binary tree with given root . ; lh will store height of left subtree rh will store height of right subtree ; f is a flag whose value helps in printing left & right part of the diameter only once ; Print the left part of the diameter ; Print the right part of the diameter ; Driver code ; Enter the binary tree ... 1 / \ 2 3 / \ 4 5 \ / \ 8 6 7 / 9
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def height ( root ) : NEW_LINE INDENT global ans , k , lh , rh , f NEW_LINE if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left_height = height ( root . left ) NEW_LINE right_height = height ( root . right ) NEW_LINE if ( ans < 1 + left_height + right_height ) : NEW_LINE INDENT ans = 1 + left_height + right_height NEW_LINE k = root NEW_LINE lh = left_height NEW_LINE rh = right_height NEW_LINE DEDENT return 1 + max ( left_height , right_height ) NEW_LINE DEDENT def printArray ( ints , lenn , f ) : NEW_LINE INDENT if ( f == 0 ) : NEW_LINE INDENT for i in range ( lenn - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( ints [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT elif ( f == 1 ) : NEW_LINE INDENT for i in range ( lenn ) : NEW_LINE INDENT print ( ints [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def printPathsRecur ( node , path , maxm , pathlen ) : NEW_LINE INDENT global f NEW_LINE if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT path [ pathlen ] = node . data NEW_LINE pathlen += 1 NEW_LINE if ( node . left == None and node . right == None ) : NEW_LINE INDENT if ( pathlen == maxm and ( f == 0 or f == 1 ) ) : NEW_LINE INDENT printArray ( path , pathlen , f ) NEW_LINE f = 2 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT printPathsRecur ( node . left , path , maxm , pathlen ) NEW_LINE printPathsRecur ( node . right , path , maxm , pathlen ) NEW_LINE DEDENT DEDENT def diameter ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT global ans , lh , rh NEW_LINE global f , k , pathLen NEW_LINE height_of_tree = height ( root ) NEW_LINE lPath = [ 0 for i in range ( 100 ) ] NEW_LINE printPathsRecur ( k . left , lPath , lh , 0 ) ; NEW_LINE print ( k . data , end = " ▁ " ) NEW_LINE rPath = [ 0 for i in range ( 100 ) ] NEW_LINE f = 1 NEW_LINE printPathsRecur ( k . right , rPath , rh , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k , lh , rh , f , ans , pathLen = None , 0 , 0 , 0 , 0 - 10 ** 19 , 0 NEW_LINE 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 . left . right . left = Node ( 6 ) NEW_LINE root . left . right . right = Node ( 7 ) NEW_LINE root . left . left . right = Node ( 8 ) NEW_LINE root . left . left . right . left = Node ( 9 ) NEW_LINE diameter ( root ) NEW_LINE DEDENT