Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree | Java implementation of the approach ; Structure of binary tree ; Function to create a new node ; Function to store the path from root node to given node of the tree in path vector and then returns true if the path exists otherwise false ; Function to print the minimum and the maximum value present in the path connecting the given two nodes of the given binary tree ; To store the path from the root node to a ; To store the path from the root node to b ; To store the minimum and the maximum value in the path from LCA to a ; To store the minimum and the maximum value in the path from LCA to b ; If both a and b are present in the tree ; Compare the paths to get the first different value ; Find minimum and maximum value in the path from LCA to a ; Find minimum and maximum value in the path from LCA to b ; Minimum of min values in first path and second path ; Maximum of max values in first path and second path ; If no path exists ; Driver Code
import java . util . * ; class GFG { static class Node { Node left ; Node right ; int data ; } ; static Node newNode ( int key ) { Node node = new Node ( ) ; node . left = node . right = null ; node . data = key ; return node ; } static Vector < Integer > path ; static boolean FindPath ( Node root , int key ) { if ( root == null ) return false ; path . add ( root . data ) ; if ( root . data == key ) return true ; if ( FindPath ( root . left , key ) || FindPath ( root . right , key ) ) return true ; path . remove ( path . size ( ) - 1 ) ; return false ; } static int minMaxNodeInPath ( Node root , int a , int b ) { path = new Vector < Integer > ( ) ; boolean flag = true ; Vector < Integer > Path2 = new Vector < Integer > ( ) , Path1 = new Vector < Integer > ( ) ; int min1 = Integer . MAX_VALUE ; int max1 = Integer . MIN_VALUE ; int min2 = Integer . MAX_VALUE ; int max2 = Integer . MIN_VALUE ; int i = 0 ; int j = 0 ; flag = FindPath ( root , a ) ; Path1 = path ; path = new Vector < Integer > ( ) ; flag &= FindPath ( root , b ) ; Path2 = path ; if ( flag ) { for ( i = 0 ; i < Path1 . size ( ) && i < Path2 . size ( ) ; i ++ ) if ( Path1 . get ( i ) != Path2 . get ( i ) ) break ; i -- ; j = i ; for ( ; i < Path1 . size ( ) ; i ++ ) { if ( min1 > Path1 . get ( i ) ) min1 = Path1 . get ( i ) ; if ( max1 < Path1 . get ( i ) ) max1 = Path1 . get ( i ) ; } for ( ; j < Path2 . size ( ) ; j ++ ) { if ( min2 > Path2 . get ( j ) ) min2 = Path2 . get ( j ) ; if ( max2 < Path2 . get ( j ) ) max2 = Path2 . get ( j ) ; } System . out . println ( " Min ▁ = ▁ " + Math . min ( min1 , min2 ) ) ; System . out . println ( " Max ▁ = ▁ " + Math . max ( max1 , max2 ) ) ; } else System . out . println ( "Min = -1 Max = - 1 "); return 0 ; } public static void main ( String args [ ] ) { Node root = newNode ( 20 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 22 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 25 ) ; root . left . right . left = newNode ( 10 ) ; root . left . right . right = newNode ( 14 ) ; int a = 5 ; int b = 14 ; minMaxNodeInPath ( root , a , b ) ; } }