Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree | Python3 implementation of the approach ; 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
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def FindPath ( root , path , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return False NEW_LINE DEDENT path . append ( root . data ) NEW_LINE if root . data == key : NEW_LINE INDENT return True NEW_LINE DEDENT if ( FindPath ( root . left , path , key ) or FindPath ( root . right , path , key ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT path . pop ( ) NEW_LINE return False NEW_LINE DEDENT def minMaxNodeInPath ( root , a , b ) : NEW_LINE INDENT Path1 = [ ] NEW_LINE Path2 = [ ] NEW_LINE min1 , max1 = float ( ' inf ' ) , float ( ' - inf ' ) NEW_LINE min2 , max2 = float ( ' inf ' ) , float ( ' - inf ' ) NEW_LINE i , j = 0 , 0 NEW_LINE if ( FindPath ( root , Path1 , a ) and FindPath ( root , Path2 , b ) ) : NEW_LINE INDENT while i < len ( Path1 ) and i < len ( Path2 ) : NEW_LINE INDENT if Path1 [ i ] != Path2 [ i ] : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT i -= 1 NEW_LINE j = i NEW_LINE while i < len ( Path1 ) : NEW_LINE INDENT if min1 > Path1 [ i ] : NEW_LINE INDENT min1 = Path1 [ i ] NEW_LINE DEDENT if max1 < Path1 [ i ] : NEW_LINE INDENT max1 = Path1 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT while j < len ( Path2 ) : NEW_LINE INDENT if min2 > Path2 [ j ] : NEW_LINE INDENT min2 = Path2 [ j ] NEW_LINE DEDENT if max2 < Path2 [ j ] : NEW_LINE INDENT max2 = Path2 [ j ] NEW_LINE DEDENT j += 1 NEW_LINE DEDENT print ( " Min ▁ = " , min ( min1 , min2 ) ) NEW_LINE print ( " Max ▁ = " , max ( max1 , max2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Min = - 1 Max = - 1 " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 25 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE a , b = 5 , 14 NEW_LINE minMaxNodeInPath ( root , a , b ) NEW_LINE DEDENT