Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree | C # 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
using System ; using System . Collections ; class GFG { class Node { public Node left ; public Node right ; public int data ; } ; static Node newNode ( int key ) { Node node = new Node ( ) ; node . left = node . right = null ; node . data = key ; return node ; } static ArrayList path ; static bool 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 ( ( int ) path [ path . Count - 1 ] ) ; return false ; } static int minMaxNodeInPath ( Node root , int a , int b ) { path = new ArrayList ( ) ; bool flag = true ; ArrayList Path2 = new ArrayList ( ) ; ArrayList Path1 = new ArrayList ( ) ; int min1 = Int32 . MaxValue ; int max1 = Int32 . MinValue ; int min2 = Int32 . MaxValue ; int max2 = Int32 . MinValue ; int i = 0 ; int j = 0 ; flag = FindPath ( root , a ) ; Path1 = path ; path = new ArrayList ( ) ; flag &= FindPath ( root , b ) ; Path2 = path ; if ( flag ) { for ( i = 0 ; i < Path1 . Count && i < Path2 . Count ; i ++ ) if ( ( int ) Path1 [ i ] != ( int ) Path2 [ i ] ) break ; i -- ; j = i ; for ( ; i < Path1 . Count ; i ++ ) { if ( min1 > ( int ) Path1 [ i ] ) min1 = ( int ) Path1 [ i ] ; if ( max1 < ( int ) Path1 [ i ] ) max1 = ( int ) Path1 [ i ] ; } for ( ; j < Path2 . Count ; j ++ ) { if ( min2 > ( int ) Path2 [ j ] ) min2 = ( int ) Path2 [ j ] ; if ( max2 < ( int ) Path2 [ j ] ) max2 = ( int ) Path2 [ j ] ; } Console . Write ( " Min ▁ = ▁ " + Math . Min ( min1 , min2 ) + " STRNEWLINE " ) ; Console . Write ( " Max ▁ = ▁ " + Math . Max ( max1 , max2 ) + " STRNEWLINE " ) ; } else Console . Write ( " Max = - 1 " ) ; return 0 ; } public static void Main ( string [ ] arg ) { 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 ) ; } }