Leaf nodes from Preorder of a Binary Search Tree | Stack based C ++ program to print leaf nodes from preorder traversal . ; Print the leaf node from the given preorder of BST . ; Since rightmost element is always leaf node . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void leafNode ( int preorder [ ] , int n ) { stack < int > s ; for ( int i = 0 , j = 1 ; j < n ; i ++ , j ++ ) { bool found = false ; if ( preorder [ i ] > preorder [ j ] ) s . push ( preorder [ i ] ) ; else { while ( ! s . empty ( ) ) { if ( preorder [ j ] > s . top ( ) ) { s . pop ( ) ; found = true ; } else break ; } } if ( found ) cout << preorder [ i ] << " ▁ " ; } cout << preorder [ n - 1 ] ; } int main ( ) { int preorder [ ] = { 890 , 325 , 290 , 530 , 965 } ; int n = sizeof ( preorder ) / sizeof ( preorder [ 0 ] ) ; leafNode ( preorder , n ) ; return 0 ; }