codeparrot/xlcost-text-to-code huggingface dataset:
Find the closest element in Binary Search Tree | Recursive C ++ program to find key closest to k in given Binary Search Tree . ; A binary tree node has key , pointer to left child and a pointer to right child ; Utility that allocates a new node with the given key and NULL left and right pointers . ; Function to find node with minimum absolute difference with given K min_diff -- > minimum difference till now min_diff_key -- > node having minimum absolute difference with K ; If k itself is present ; update min_diff and min_diff_key by checking current node value ; if k is less than ptr -> key then move in left subtree else in right subtree ; Wrapper over maxDiffUtil ( ) ; Initialize minimum difference ; Find value of min_diff_key ( Closest key in tree with k ) ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newnode ( int key ) { struct Node * node = new ( struct Node ) ; node -> key = key ; node -> left = node -> right = NULL ; return ( node ) ; } void maxDiffUtil ( struct Node * ptr , int k , int & min_diff , int & min_diff_key ) { if ( ptr == NULL ) return ; if ( ptr -> key == k ) { min_diff_key = k ; return ; } if ( min_diff > abs ( ptr -> key - k ) ) { min_diff = abs ( ptr -> key - k ) ; min_diff_key = ptr -> key ; } if ( k < ptr -> key ) maxDiffUtil ( ptr -> left , k , min_diff , min_diff_key ) ; else maxDiffUtil ( ptr -> right , k , min_diff , min_diff_key ) ; } int maxDiff ( Node * root , int k ) { int min_diff = INT_MAX , min_diff_key = -1 ; maxDiffUtil ( root , k , min_diff , min_diff_key ) ; return min_diff_key ; } int main ( ) { struct Node * root = newnode ( 9 ) ; root -> left = newnode ( 4 ) ; root -> right = newnode ( 17 ) ; root -> left -> left = newnode ( 3 ) ; root -> left -> right = newnode ( 6 ) ; root -> left -> right -> left = newnode ( 5 ) ; root -> left -> right -> right = newnode ( 7 ) ; root -> right -> right = newnode ( 22 ) ; root -> right -> right -> left = newnode ( 20 ) ; int k = 18 ; cout << maxDiff ( root , k ) ; return 0 ; }
Construct a complete binary tree from given array in level order fashion | CPP program to construct binary tree from given array in level order fashion Tree Node ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node ; Function to insert nodes in level order ; Base case for recursion ; insert left child ; insert right child ; Function to print tree nodes in InOrder fashion ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = ( Node * ) malloc ( sizeof ( Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } Node * insertLevelOrder ( int arr [ ] , Node * root , int i , int n ) { if ( i < n ) { Node * temp = newNode ( arr [ i ] ) ; root = temp ; root -> left = insertLevelOrder ( arr , root -> left , 2 * i + 1 , n ) ; root -> right = insertLevelOrder ( arr , root -> right , 2 * i + 2 , n ) ; } return root ; } void inOrder ( Node * root ) { if ( root != NULL ) { inOrder ( root -> left ) ; cout << root -> data << " ▁ " ; inOrder ( root -> right ) ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Node * root = insertLevelOrder ( arr , root , 0 , n ) ; inOrder ( root ) ; }
Construct Full Binary Tree from given preorder and postorder traversals | program for construction of full binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A utility function to create a node ; A recursive function to construct Full from pre [ ] and post [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in post [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur ; Search the next element of pre [ ] in post [ ] ; Use the index of element found in postorder to divide postorder array in two parts . Left subtree and right subtree ; The main function to construct Full Binary Tree from given preorder and postorder traversals . This function mainly uses constructTreeUtil ( ) ; A utility function to print inorder traversal of a Binary Tree ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; node * newNode ( int data ) { node * temp = new node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } node * constructTreeUtil ( int pre [ ] , int post [ ] , int * preIndex , int l , int h , int size ) { if ( * preIndex >= size l > h ) return NULL ; node * root = newNode ( pre [ * preIndex ] ) ; ++ * preIndex ; if ( l == h ) return root ; int i ; for ( i = l ; i <= h ; ++ i ) if ( pre [ * preIndex ] == post [ i ] ) break ; if ( i <= h ) { root -> left = constructTreeUtil ( pre , post , preIndex , l , i , size ) ; root -> right = constructTreeUtil ( pre , post , preIndex , i + 1 , h , size ) ; } return root ; } node * constructTree ( int pre [ ] , int post [ ] , int size ) { int preIndex = 0 ; return constructTreeUtil ( pre , post , & preIndex , 0 , size - 1 , size ) ; } void printInorder ( node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; cout << node -> data << " ▁ " ; printInorder ( node -> right ) ; } int main ( ) { int pre [ ] = { 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 } ; int post [ ] = { 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int size = sizeof ( pre ) / sizeof ( pre [ 0 ] ) ; node * root = constructTree ( pre , post , size ) ; cout << " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree : ▁ STRNEWLINE " ; printInorder ( root ) ; return 0 ; }
Convert a Binary Tree to Threaded binary tree | Set 2 ( Efficient ) | C ++ program to convert a Binary Tree to Threaded Tree ; Structure of a node in threaded binary tree ; Used to indicate whether the right pointer is a normal right pointer or a pointer to inorder successor . ; Converts tree with given root to threaded binary tree . This function returns rightmost child of root . ; Base cases : Tree is empty or has single node ; Find predecessor if it exists ; Find predecessor of root ( Rightmost child in left subtree ) ; Link a thread from predecessor to root . ; If current node is rightmost child ; Recur for right subtree . ; A utility function to find leftmost node in a binary tree rooted with ' root ' . This function is used in inOrder ( ) ; Function to do inorder traversal of a threadded binary tree ; Find the leftmost node in Binary Tree ; If this Node is a thread Node , then go to inorder successor ; Else go to the leftmost child in right subtree ; A utility function to create a new node ; Driver program to test above functions ; 1 / \ 2 3 / \ / \ 4 5 6 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; Node * left , * right ; bool isThreaded ; } ; Node * createThreaded ( Node * root ) { if ( root == NULL ) return NULL ; if ( root -> left == NULL && root -> right == NULL ) return root ; if ( root -> left != NULL ) { Node * l = createThreaded ( root -> left ) ; l -> right = root ; l -> isThreaded = true ; } if ( root -> right == NULL ) return root ; return createThreaded ( root -> right ) ; } Node * leftMost ( Node * root ) { while ( root != NULL && root -> left != NULL ) root = root -> left ; return root ; } void inOrder ( Node * root ) { if ( root == NULL ) return ; Node * cur = leftMost ( root ) ; while ( cur != NULL ) { cout << cur -> key << " ▁ " ; if ( cur -> isThreaded ) cur = cur -> right ; else cur = leftMost ( cur -> right ) ; } } Node * newNode ( int key ) { Node * temp = new Node ; temp -> left = temp -> right = NULL ; temp -> key = key ; return temp ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; createThreaded ( root ) ; cout << " Inorder ▁ traversal ▁ of ▁ created ▁ " " threaded ▁ tree ▁ is STRNEWLINE " ; inOrder ( root ) ; return 0 ; }
Inorder Non | C ++ program to print inorder traversal of a Binary Search Tree ( BST ) without recursion and stack ; BST Node ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Function to print inorder traversal using parent pointer ; Start traversal from root ; If left child is not traversed , find the leftmost child ; Print root 's data ; Mark left as done ; If right child exists ; If right child doesn 't exist, move to parent ; If this node is right child of its parent , visit parent 's parent first ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { Node * left , * right , * parent ; int key ; } ; Node * newNode ( int item ) { Node * temp = new Node ; temp -> key = item ; temp -> parent = temp -> left = temp -> right = NULL ; return temp ; } Node * insert ( Node * node , int key ) { if ( node == NULL ) return newNode ( key ) ; if ( key < node -> key ) { node -> left = insert ( node -> left , key ) ; node -> left -> parent = node ; } else if ( key > node -> key ) { node -> right = insert ( node -> right , key ) ; node -> right -> parent = node ; } return node ; } void inorder ( Node * root ) { bool leftdone = false ; while ( root ) { if ( ! leftdone ) { while ( root -> left ) root = root -> left ; } printf ( " % d ▁ " , root -> key ) ; leftdone = true ; if ( root -> right ) { leftdone = false ; root = root -> right ; } else if ( root -> parent ) { while ( root -> parent && root == root -> parent -> right ) root = root -> parent ; if ( ! root -> parent ) break ; root = root -> parent ; } else break ; } } int main ( void ) { Node * root = NULL ; root = insert ( root , 24 ) ; root = insert ( root , 27 ) ; root = insert ( root , 29 ) ; root = insert ( root , 34 ) ; root = insert ( root , 14 ) ; root = insert ( root , 4 ) ; root = insert ( root , 10 ) ; root = insert ( root , 22 ) ; root = insert ( root , 13 ) ; root = insert ( root , 3 ) ; root = insert ( root , 2 ) ; root = insert ( root , 6 ) ; printf ( " Inorder ▁ traversal ▁ is ▁ STRNEWLINE " ) ; inorder ( root ) ; return 0 ; }
Sorted order printing of a given array that represents a BST | C ++ Code for Sorted order printing of a given array that represents a BST ; print left subtree ; print root ; print right subtree ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSorted ( int arr [ ] , int start , int end ) { if ( start > end ) return ; printSorted ( arr , start * 2 + 1 , end ) ; cout << arr [ start ] << " ▁ " ; printSorted ( arr , start * 2 + 2 , end ) ; } int main ( ) { int arr [ ] = { 4 , 2 , 5 , 1 , 3 } ; int arr_size = sizeof ( arr ) / sizeof ( int ) ; printSorted ( arr , 0 , arr_size - 1 ) ; getchar ( ) ; return 0 ; }
Floor and Ceil from a BST | Program to find ceil of a given value in BST ; A binary tree node has key , left child and right child ; Helper function that allocates a new node with the given key and NULL left and right pointers . ; Function to find ceil of a given input in BST . If input is more than the max key in BST , return - 1 ; Base case ; We found equal key ; If root 's key is smaller, ceil must be in right subtree ; Else , either left subtree or root has the ceil value ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int key ; node * left ; node * right ; } ; node * newNode ( int key ) { node * Node = new node ( ) ; Node -> key = key ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int Ceil ( node * root , int input ) { if ( root == NULL ) return -1 ; if ( root -> key == input ) return root -> key ; if ( root -> key < input ) return Ceil ( root -> right , input ) ; int ceil = Ceil ( root -> left , input ) ; return ( ceil >= input ) ? ceil : root -> key ; } int main ( ) { node * root = newNode ( 8 ) ; root -> left = newNode ( 4 ) ; root -> right = newNode ( 12 ) ; root -> left -> left = newNode ( 2 ) ; root -> left -> right = newNode ( 6 ) ; root -> right -> left = newNode ( 10 ) ; root -> right -> right = newNode ( 14 ) ; for ( int i = 0 ; i < 16 ; i ++ ) cout << i << " ▁ " << Ceil ( root , i ) << endl ; return 0 ; }
Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree | C ++ program to construct full binary tree using its preorder traversal and preorder traversal of its mirror tree ; A Binary Tree Node ; Utility function to create a new tree node ; A utility function to print inorder traversal of a Binary Tree ; A recursive function to construct Full binary tree from pre [ ] and preM [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in preM [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur ; Search the next element of pre [ ] in preM [ ] ; construct left and right subtrees recursively ; return root ; function to construct full binary tree using its preorder traversal and preorder traversal of its mirror tree ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void printInorder ( Node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; printf ( " % d ▁ " , node -> data ) ; printInorder ( node -> right ) ; } Node * constructBinaryTreeUtil ( int pre [ ] , int preM [ ] , int & preIndex , int l , int h , int size ) { if ( preIndex >= size l > h ) return NULL ; Node * root = newNode ( pre [ preIndex ] ) ; ++ ( preIndex ) ; if ( l == h ) return root ; int i ; for ( i = l ; i <= h ; ++ i ) if ( pre [ preIndex ] == preM [ i ] ) break ; if ( i <= h ) { root -> left = constructBinaryTreeUtil ( pre , preM , preIndex , i , h , size ) ; root -> right = constructBinaryTreeUtil ( pre , preM , preIndex , l + 1 , i - 1 , size ) ; } return root ; } void constructBinaryTree ( Node * root , int pre [ ] , int preMirror [ ] , int size ) { int preIndex = 0 ; int preMIndex = 0 ; root = constructBinaryTreeUtil ( pre , preMirror , preIndex , 0 , size - 1 , size ) ; printInorder ( root ) ; } int main ( ) { int preOrder [ ] = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int preOrderMirror [ ] = { 1 , 3 , 7 , 6 , 2 , 5 , 4 } ; int size = sizeof ( preOrder ) / sizeof ( preOrder [ 0 ] ) ; Node * root = new Node ; constructBinaryTree ( root , preOrder , preOrderMirror , size ) ; return 0 ; }
Floor and Ceil from a BST | C ++ program to find floor and ceil of a given key in BST ; A binary tree node has key , left child and right child ; Helper function to find floor and ceil of a given key in BST ; Display the floor and ceil of a given key in BST . If key is less than the min key in BST , floor will be - 1 ; If key is more than the max key in BST , ceil will be - 1 ; ; Variables ' floor ' and ' ceil ' are passed by reference ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; Node ( int value ) { data = value ; left = right = NULL ; } } ; void floorCeilBSTHelper ( Node * root , int key , int & floor , int & ceil ) { while ( root ) { if ( root -> data == key ) { ceil = root -> data ; floor = root -> data ; return ; } if ( key > root -> data ) { floor = root -> data ; root = root -> right ; } else { ceil = root -> data ; root = root -> left ; } } return ; } void floorCeilBST ( Node * root , int key ) { int floor = -1 , ceil = -1 ; floorCeilBSTHelper ( root , key , floor , ceil ) ; cout << key << ' ▁ ' << floor << ' ▁ ' << ceil << ' ' ; } int main ( ) { Node * root = new Node ( 8 ) ; root -> left = new Node ( 4 ) ; root -> right = new Node ( 12 ) ; root -> left -> left = new Node ( 2 ) ; root -> left -> right = new Node ( 6 ) ; root -> right -> left = new Node ( 10 ) ; root -> right -> right = new Node ( 14 ) ; for ( int i = 0 ; i < 16 ; i ++ ) floorCeilBST ( root , i ) ; return 0 ; }
Print Common Nodes in Two Binary Search Trees | C ++ program of iterative traversal based method to find common elements in two BSTs . ; A BST node ; A utility function to create a new node ; Function two print common elements in given two trees ; Create two stacks for two inorder traversals ; push the Nodes of first tree in stack s1 ; push the Nodes of second tree in stack s2 ; Both root1 and root2 are NULL here ; If current keys in two trees are same ; move to the inorder successor ; If Node of first tree is smaller , than that of second tree , then its obvious that the inorder successors of current Node can have same value as that of the second tree Node . Thus , we pop from s2 ; root2 is set to NULL , because we need new Nodes of tree 1 ; Both roots and both stacks are empty ; A utility function to do inorder traversal ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; return the ( unchanged ) Node pointer ; Driver program ; Create first tree as shown in example ; Create second tree as shown in example
#include <iostream> NEW_LINE #include <stack> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int ele ) { Node * temp = new Node ; temp -> key = ele ; temp -> left = temp -> right = NULL ; return temp ; } void printCommon ( Node * root1 , Node * root2 ) { stack < Node * > stack1 , s1 , s2 ; while ( 1 ) { if ( root1 ) { s1 . push ( root1 ) ; root1 = root1 -> left ; } else if ( root2 ) { s2 . push ( root2 ) ; root2 = root2 -> left ; } else if ( ! s1 . empty ( ) && ! s2 . empty ( ) ) { root1 = s1 . top ( ) ; root2 = s2 . top ( ) ; if ( root1 -> key == root2 -> key ) { cout << root1 -> key << " ▁ " ; s1 . pop ( ) ; s2 . pop ( ) ; root1 = root1 -> right ; root2 = root2 -> right ; } else if ( root1 -> key < root2 -> key ) { s1 . pop ( ) ; root1 = root1 -> right ; root2 = NULL ; } else if ( root1 -> key > root2 -> key ) { s2 . pop ( ) ; root2 = root2 -> right ; root1 = NULL ; } } else break ; } } void inorder ( struct Node * root ) { if ( root ) { inorder ( root -> left ) ; cout << root -> key << " ▁ " ; inorder ( root -> right ) ; } } struct Node * insert ( struct Node * node , int key ) { if ( node == NULL ) return newNode ( key ) ; if ( key < node -> key ) node -> left = insert ( node -> left , key ) ; else if ( key > node -> key ) node -> right = insert ( node -> right , key ) ; return node ; } int main ( ) { Node * root1 = NULL ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 1 ) ; root1 = insert ( root1 , 10 ) ; root1 = insert ( root1 , 0 ) ; root1 = insert ( root1 , 4 ) ; root1 = insert ( root1 , 7 ) ; root1 = insert ( root1 , 9 ) ; Node * root2 = NULL ; root2 = insert ( root2 , 10 ) ; root2 = insert ( root2 , 7 ) ; root2 = insert ( root2 , 20 ) ; root2 = insert ( root2 , 4 ) ; root2 = insert ( root2 , 9 ) ; cout << " Tree ▁ 1 ▁ : ▁ " ; inorder ( root1 ) ; cout << endl ; cout << " Tree ▁ 2 ▁ : ▁ " ; inorder ( root2 ) ; cout << " Common Nodes : " printCommon ( root1 , root2 ) ; return 0 ; }
Leaf nodes from Preorder of a Binary Search Tree | C ++ program to print leaf node from preorder of binary search tree . ; Binary Search ; Point to the index in preorder . ; Function to print Leaf Nodes by doing preorder traversal of tree using preorder and inorder arrays . ; If l == r , therefore no right or left subtree . So , it must be leaf Node , print it . ; If array is out of bound , return . ; Finding the index of preorder element in inorder array using binary search . ; Incrementing the index . ; Finding on the left subtree . ; Finding on the right subtree . ; Finds leaf nodes from given preorder traversal . ; To store inorder traversal ; Copy the preorder into another array . ; Finding the inorder by sorting the array . ; Print the Leaf Nodes . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySearch ( int inorder [ ] , int l , int r , int d ) { int mid = ( l + r ) >> 1 ; if ( inorder [ mid ] == d ) return mid ; else if ( inorder [ mid ] > d ) return binarySearch ( inorder , l , mid - 1 , d ) ; else return binarySearch ( inorder , mid + 1 , r , d ) ; } int ind = 0 ; void leafNodesRec ( int preorder [ ] , int inorder [ ] , int l , int r , int * ind , int n ) { if ( l == r ) { printf ( " % d ▁ " , inorder [ l ] ) ; * ind = * ind + 1 ; return ; } if ( l < 0 l > r r >= n ) return ; int loc = binarySearch ( inorder , l , r , preorder [ * ind ] ) ; * ind = * ind + 1 ; leafNodesRec ( preorder , inorder , l , loc - 1 , ind , n ) ; leafNodesRec ( preorder , inorder , loc + 1 , r , ind , n ) ; } void leafNodes ( int preorder [ ] , int n ) { int inorder [ n ] ; for ( int i = 0 ; i < n ; i ++ ) inorder [ i ] = preorder [ i ] ; sort ( inorder , inorder + n ) ; leafNodesRec ( preorder , inorder , 0 , n - 1 , & ind , n ) ; } int main ( ) { int preorder [ ] = { 890 , 325 , 290 , 530 , 965 } ; int n = sizeof ( preorder ) / sizeof ( preorder [ 0 ] ) ; leafNodes ( preorder , n ) ; return 0 ; }
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 ; }
Leaf nodes from Preorder of a Binary Search Tree ( Using Recursion ) | Recursive C ++ program to find leaf nodes from given preorder traversal ; Print the leaf node from the given preorder of BST . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isLeaf ( int pre [ ] , int & i , int n , int min , int max ) { if ( i >= n ) return false ; if ( pre [ i ] > min && pre [ i ] < max ) { i ++ ; bool left = isLeaf ( pre , i , n , min , pre [ i - 1 ] ) ; bool right = isLeaf ( pre , i , n , pre [ i - 1 ] , max ) ; if ( ! left && ! right ) cout << pre [ i - 1 ] << " ▁ " ; return true ; } return false ; } void printLeaves ( int preorder [ ] , int n ) { int i = 0 ; isLeaf ( preorder , i , n , INT_MIN , INT_MAX ) ; } int main ( ) { int preorder [ ] = { 890 , 325 , 290 , 530 , 965 } ; int n = sizeof ( preorder ) / sizeof ( preorder [ 0 ] ) ; printLeaves ( preorder , n ) ; return 0 ; }