Leaf nodes from Preorder of a Binary Search Tree ( Using Recursion ) | Recursive Javascript program to find leaf nodes from given preorder traversal ; Print the leaf node from the given preorder of BST . ; Driver code
let i = 0 ; function isLeaf ( pre , n , min , max ) { if ( i >= n ) { return false ; } if ( pre [ i ] > min && pre [ i ] < max ) { i ++ ; let left = isLeaf ( pre , n , min , pre [ i - 1 ] ) ; let right = isLeaf ( pre , n , pre [ i - 1 ] , max ) ; if ( ! left && ! right ) { document . write ( pre [ i - 1 ] + " " ) ; } return true ; } return false ; } function printLeaves ( preorder , n ) { isLeaf ( preorder , n , Number . MIN_VALUE , Number . MAX_VALUE ) ; } let preorder = [ 890 , 325 , 290 , 530 , 965 ] ; let n = preorder . length ; printLeaves ( preorder , n ) ;