# Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)

Given Preorder traversal of a Binary Search Tree. Then the task is print leaf nodes of the Binary Search Tree from the given preorder.

Examples :

```Input : preorder[] = {890, 325, 290, 530, 965};
Output : 290 530 965

Tree represented is,
890
/   \
325    965
/  \
290   530

Input :  preorder[] = { 3, 2, 4 };
Output : 2 4
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

In this post, a simple recursive solution is discussed. The idea is to use two min and max variables and taking i (index in input array), the index for given preorder array, and recursively creating root node and correspondingly checking if left and right are existing or not. This method return boolean variable, and if both left and right are false it simply means that left and right are null hence it must be a leaf node so print it right there and return back true as root at that index existed.

 `// Recursive C++ program  to find leaf  ` `// nodes from given preorder traversal ` `#include ` `using` `namespace` `std; ` ` `  `// Print the leaf node from  ` `// the given preorder of BST. ` `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); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `preorder[] = { 890, 325, 290, 530, 965 }; ` `    ``int` `n = ``sizeof``(preorder)/``sizeof``(preorder); ` `    ``printLeaves(preorder, n);     ` `    ``return` `0; ` `} `

 `// Recursive Java program to find leaf  ` `// nodes from given preorder traversal ` `class` `GFG  ` `{ ` ` `  `    ``static` `int` `i = ``0``; ` ` `  `    ``// Print the leaf node from  ` `    ``// the given preorder of BST. ` `    ``static` `boolean` `isLeaf(``int` `pre[], ``int` `n, ` `            ``int` `min, ``int` `max) ` `    ``{ ` `        ``if` `(i >= n) ` `        ``{ ` `            ``return` `false``; ` `        ``} ` ` `  `        ``if` `(pre[i] > min && pre[i] < max)  ` `        ``{ ` `            ``i++; ` ` `  `            ``boolean` `left = isLeaf(pre, n, min, pre[i - ``1``]); ` `            ``boolean` `right = isLeaf(pre, n, pre[i - ``1``], max); ` ` `  `            ``if` `(!left && !right)  ` `            ``{ ` `                ``System.out.print(pre[i - ``1``] + ``" "``); ` `            ``} ` ` `  `            ``return` `true``; ` `        ``} ` `        ``return` `false``; ` `    ``} ` ` `  `    ``static` `void` `printLeaves(``int` `preorder[], ``int` `n)  ` `    ``{ ` ` `  `        ``isLeaf(preorder, n, Integer.MIN_VALUE, ` `                            ``Integer.MAX_VALUE); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `preorder[] = {``890``, ``325``, ``290``, ``530``, ``965``}; ` `        ``int` `n = preorder.length; ` `        ``printLeaves(preorder, n); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

 `# Recursive Python program to find leaf  ` `# nodes from given preorder traversal  ` ` `  `# Print the leaf node from  ` `# the given preorder of BST.  ` `def` `isLeaf(pre, i, n, ``Min``, ``Max``): ` `    ``if` `i[``0``] >``=` `n:  ` `        ``return` `False` `     `  `    ``if` `pre[i[``0``]] > ``Min` `and` `pre[i[``0``]] < ``Max``:  ` `        ``i[``0``] ``+``=` `1` `         `  `        ``left ``=` `isLeaf(pre, i, n, ``Min``,  ` `                      ``pre[i[``0``] ``-` `1``])  ` `        ``right ``=` `isLeaf(pre, i, n,  ` `                       ``pre[i[``0``] ``-` `1``], ``Max``)  ` `         `  `        ``if` `left ``=``=` `False` `and` `right ``=``=` `False``: ` `            ``print``(pre[i[``0``] ``-` `1``], end ``=` `" "``) ` `             `  `        ``return` `True` `    ``return` `False` ` `  `def` `printLeaves(preorder, n): ` `    ``i ``=` `[``0``] ` `    ``INT_MIN, INT_MAX ``=` `-``999999999999``, ``999999999999` `    ``isLeaf(preorder, i, n, INT_MIN, INT_MAX) ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``preorder ``=` `[``890``, ``325``, ``290``, ``530``, ``965``]  ` `    ``n ``=` `len``(preorder)  ` `    ``printLeaves(preorder, n) ` `     `  `# This code is contributed by PranchalK `

 `// Recursive C# program to find leaf  ` `// nodes from given preorder traversal  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `    ``static` `int` `i = 0;  ` ` `  `    ``// Print the leaf node from  ` `    ``// the given preorder of BST.  ` `    ``static` `bool` `isLeaf(``int` `[]pre, ``int` `n,  ` `                        ``int` `min, ``int` `max)  ` `    ``{  ` `        ``if` `(i >= n)  ` `        ``{  ` `            ``return` `false``;  ` `        ``}  ` ` `  `        ``if` `(pre[i] > min && pre[i] < max)  ` `        ``{  ` `            ``i++;  ` ` `  `            ``bool` `left = isLeaf(pre, n, min, pre[i - 1]);  ` `            ``bool` `right = isLeaf(pre, n, pre[i - 1], max);  ` ` `  `            ``if` `(!left && !right)  ` `            ``{  ` `                ``Console.Write(pre[i - 1] + ``" "``);  ` `            ``}  ` ` `  `            ``return` `true``;  ` `        ``}  ` `        ``return` `false``;  ` `    ``}  ` ` `  `    ``static` `void` `printLeaves(``int` `[]preorder, ``int` `n)  ` `    ``{  ` ` `  `        ``isLeaf(preorder, n, ``int``.MinValue, ``int``.MaxValue);  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{  ` `        ``int` `[]preorder = {890, 325, 290, 530, 965};  ` `        ``int` `n = preorder.Length;  ` `        ``printLeaves(preorder, n);  ` `    ``}  ` `}  ` ` `  `// This code is contributed by princiraj1992 `

 `= ``\$n``)  ` `        ``return` `false; ` `     `  `    ``if` `(``\$pre``[``\$i``] > ``\$min` `&&  ` `        ``\$pre``[``\$i``] < ``\$max``)  ` `    ``{ ` `        ``\$i``++; ` `         `  `        ``\$left` `= isLeaf(``\$pre``, ``\$i``, ``\$n``,  ` `                       ``\$min``, ``\$pre``[``\$i` `- 1]); ` `        ``\$right` `= isLeaf(``\$pre``, ``\$i``, ``\$n``,  ` `                        ``\$pre``[``\$i` `- 1], ``\$max``); ` `         `  `        ``if` `(!``\$left` `&& !``\$right``)  ` `            ``echo` `\$pre``[``\$i` `- 1] , ``" "``; ` `             `  `        ``return` `true; ` `    ``} ` `    ``return` `false; ` `} ` ` `  `function` `printLeaves(``\$preorder``, ``\$n``) ` `{ ` `    ``\$i` `= 0;  ` `    ``isLeaf(``\$preorder``, ``\$i``, ``\$n``,  ` `           ``PHP_INT_MIN, PHP_INT_MAX); ` `} ` ` `  `// Driver code ` `\$preorder` `= ``array` `(890, 325, 290,  ` `                   ``530, 965 ); ` `\$n` `= sizeof(``\$preorder``); ` `printLeaves(``\$preorder``, ``\$n``);  ` ` `  `// This code is contributed by ajit ` `?> `

Output :
```290 530 965
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :