Given a preorder sequence of the binary search tree of N nodes. The task is to find its leftmost and rightmost nodes.
Examples:
Input : N = 5, preorder[]={ 3, 2, 1, 5, 4 }
Output : Leftmost = 1, Rightmost = 5
The BST constructed from this
preorder sequence would be:
3
/ \
2 5
/ /
1 4
Leftmost Node of this tree is equal to 1
Rightmost Node of this tree is equal to 5
Input : N = 3 preorder[]={ 2, 1, 3}
Output : Leftmost = 1, Rightmost = 3
Naive Approach:
Construct BST from the given preorder sequence. See this post for understanding the code to construct bst from a given preorder. After constructing the BST, find the leftmost and rightmost node by traversing from root to leftmost node and traversing from root to rightmost node.
Time Complexity: O(N)
Space Complexity: O(N)
Efficient Approach:
Instead of constructing the tree, make use of the property of BST. The leftmost node in BST always has the smallest value and the rightmost node in BST always has the largest value.
So, from the given array we just need to find the minimum value for the leftmost node and the maximum value for the rightmost node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LeftRightNode( int preorder[], int n)
{
int min = INT_MAX, max = INT_MIN;
for ( int i = 0; i < n; i++) {
if (min > preorder[i])
min = preorder[i];
if (max < preorder[i])
max = preorder[i];
}
cout << "Leftmost node is " << min << "\n" ;
cout << "Rightmost node is " << max;
}
int main()
{
int preorder[] = { 3, 2, 1, 5, 4 };
int n = 5;
LeftRightNode(preorder, n);
return 0;
}
|
Java
class GFG
{
static void LeftRightNode( int preorder[], int n)
{
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
{
if (min > preorder[i])
min = preorder[i];
if (max < preorder[i])
max = preorder[i];
}
System.out.println( "Leftmost node is " + min);
System.out.println( "Rightmost node is " + max);
}
public static void main(String[] args)
{
int preorder[] = { 3 , 2 , 1 , 5 , 4 };
int n = 5 ;
LeftRightNode(preorder, n);
}
}
|
Python3
def LeftRightNode(preorder, n):
min = 10 * * 9
max = - 10 * * 9
for i in range (n):
if ( min > preorder[i]):
min = preorder[i]
if ( max < preorder[i]):
max = preorder[i]
print ( "Leftmost node is " , min )
print ( "Rightmost node is " , max )
preorder = [ 3 , 2 , 1 , 5 , 4 ]
n = len (preorder)
LeftRightNode(preorder, n)
|
C#
using System;
class GFG
{
static void LeftRightNode( int []preorder, int n)
{
int min = int .MaxValue, max = int .MinValue;
for ( int i = 0; i < n; i++)
{
if (min > preorder[i])
min = preorder[i];
if (max < preorder[i])
max = preorder[i];
}
Console.WriteLine( "Leftmost node is " + min);
Console.WriteLine( "Rightmost node is " + max);
}
public static void Main(String[] args)
{
int []preorder = { 3, 2, 1, 5, 4 };
int n = 5;
LeftRightNode(preorder, n);
}
}
|
Javascript
<script>
function LeftRightNode(preorder, n)
{
var min = 1000000000, max = -1000000000;
for ( var i = 0; i < n; i++) {
if (min > preorder[i])
min = preorder[i];
if (max < preorder[i])
max = preorder[i];
}
document.write( "Leftmost node is " + min + "<br>" );
document.write( "Rightmost node is " + max);
}
var preorder = [3, 2, 1, 5, 4];
var n = 5;
LeftRightNode(preorder, n);
</script>
|
Output:
Leftmost node is 1
Rightmost node is 5
Time Complexity: O(N)
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 May, 2021
Like Article
Save Article