Print the longest leaf to leaf path in a Binary tree | Java program to print the longest leaf to leaf path ; 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 . ; 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
import java . io . * ; class Node { int data ; Node left , right ; Node ( int val ) { data = val ; left = right = null ; } } class GFG { static int ans , lh , rh , f ; static Node k ; public static Node Root ; static int height ( Node root ) { if ( root == null ) return 0 ; int left_height = height ( root . left ) ; int right_height = height ( root . right ) ; if ( ans < 1 + left_height + right_height ) { ans = 1 + left_height + right_height ; k = root ; lh = left_height ; rh = right_height ; } return 1 + Math . max ( left_height , right_height ) ; } static void printArray ( int [ ] ints , int len ) { int i ; if ( f == 0 ) { for ( i = len - 1 ; i >= 0 ; i -- ) { System . out . print ( ints [ i ] + " ▁ " ) ; } } else if ( f == 1 ) { for ( i = 0 ; i < len ; i ++ ) { System . out . print ( ints [ i ] + " ▁ " ) ; } } } static void printPathsRecur ( Node node , int [ ] path , int pathLen , int max ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) { if ( pathLen == max && ( f == 0 f == 1 ) ) { printArray ( path , pathLen ) ; f = 2 ; } } else { printPathsRecur ( node . left , path , pathLen , max ) ; printPathsRecur ( node . right , path , pathLen , max ) ; } } static void diameter ( Node root ) { if ( root == null ) return ; ans = Integer . MIN_VALUE ; lh = 0 ; rh = 0 ; f = 0 ; int height_of_tree = height ( root ) ; int [ ] lPath = new int [ 100 ] ; int pathlen = 0 ; printPathsRecur ( k . left , lPath , pathlen , lh ) ; System . out . print ( k . data + " ▁ " ) ; int [ ] rPath = new int [ 100 ] ; f = 1 ; printPathsRecur ( k . right , rPath , pathlen , rh ) ; } public static void main ( String [ ] args ) { GFG . Root = new Node ( 1 ) ; GFG . Root . left = new Node ( 2 ) ; GFG . Root . right = new Node ( 3 ) ; GFG . Root . left . left = new Node ( 4 ) ; GFG . Root . left . right = new Node ( 5 ) ; GFG . Root . left . right . left = new Node ( 6 ) ; GFG . Root . left . right . right = new Node ( 7 ) ; GFG . Root . left . left . right = new Node ( 8 ) ; GFG . Root . left . left . right . left = new Node ( 9 ) ; diameter ( Root ) ; } }