Find Leftmost and Rightmost node of BST from its given preorder traversal | Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code
def LeftRightNode ( preorder , n ) : NEW_LINE INDENT min = 10 ** 9 NEW_LINE max = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( min > preorder [ i ] ) : NEW_LINE INDENT min = preorder [ i ] NEW_LINE DEDENT if ( max < preorder [ i ] ) : NEW_LINE INDENT max = preorder [ i ] NEW_LINE DEDENT DEDENT print ( " Leftmost ▁ node ▁ is ▁ " , min ) NEW_LINE print ( " Rightmost ▁ node ▁ is ▁ " , max ) NEW_LINE DEDENT preorder = [ 3 , 2 , 1 , 5 , 4 ] NEW_LINE n = len ( preorder ) NEW_LINE LeftRightNode ( preorder , n ) NEW_LINE