Find Leftmost and Rightmost node of BST from its given preorder traversal

Given a preorder sequence of the binary search tree of N nodes. The task is to find its leftmost and rightmost node.

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 code to construct bst from given preorder. After constructing the BST, Find 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 rightmost node in BST always has the largest value.
So from the given array we just need to find minimum value for leftmost node and maximum value for rightmost node.

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find leftmost and
// rightmost node from given preorder sequence
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the leftmost and 
// rightmost nodes of the BST whose 
// preorder traversal is given
void LeftRightNode(int preorder[], int n)
{
    // Variables for finding minimum 
    // and maximum values of the array
    int min = INT_MAX, max = INT_MIN;
  
    for (int i = 0; i < n; i++) {
          
        // Update the minimum
        if (min > preorder[i])
            min = preorder[i];
     
        // Update the maximum
        if (max < preorder[i])
            max = preorder[i];
    }
  
    // Print the values
    cout << "Leftmost node is " << min << "\n";
    cout << "Rightmost node is " << max;
}
  
// Driver Code
int main()
{
    int preorder[] = { 3, 2, 1, 5, 4 };
    int n = 5;
  
    LeftRightNode(preorder, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find leftmost and
// rightmost node from given preorder sequence
class GFG
{
  
// Function to return the leftmost and 
// rightmost nodes of the BST whose 
// preorder traversal is given
static void LeftRightNode(int preorder[], int n)
{
    // Variables for finding minimum 
    // and maximum values of the array
    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
  
    for (int i = 0; i < n; i++)
    {
          
        // Update the minimum
        if (min > preorder[i])
            min = preorder[i];
      
        // Update the maximum
        if (max < preorder[i])
            max = preorder[i];
    }
    // Print the values
    System.out.println("Leftmost node is " + min);
    System.out.println("Rightmost node is " + max);
}
  
// Driver Code
public static void main(String[] args) 
{
        int preorder[] = { 3, 2, 1, 5, 4 };
        int n = 5;
        LeftRightNode(preorder, n);
  
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find leftmost and
# rightmost node from given preorder sequence
  
# Function to return the leftmost and 
# rightmost nodes of the BST whose 
# preorder traversal is given
def LeftRightNode(preorder, n):
    # Variables for finding minimum 
    # and maximum values of the array
    min = 10**9
    max = -10**9
  
    for i in range(n):
          
        # Update the minimum
        if (min > preorder[i]):
            min = preorder[i]
      
        # Update the maximum
        if (max < preorder[i]):
            max = preorder[i]
  
    # Print the values
    print("Leftmost node is ",min)
    print("Rightmost node is ",max)
  
# Driver Code
  
preorder = [3, 2, 1, 5, 4]
n =len(preorder)
  
LeftRightNode(preorder, n)
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find leftmost and
// rightmost node from given preorder sequence
using System;
      
class GFG
{
  
// Function to return the leftmost and 
// rightmost nodes of the BST whose 
// preorder traversal is given
static void LeftRightNode(int []preorder, int n)
{
    // Variables for finding minimum 
    // and maximum values of the array
    int min = int.MaxValue, max = int.MinValue;
  
    for (int i = 0; i < n; i++)
    {
          
        // Update the minimum
        if (min > preorder[i])
            min = preorder[i];
      
        // Update the maximum
        if (max < preorder[i])
            max = preorder[i];
    }
    // Print the values
    Console.WriteLine("Leftmost node is " + min);
    Console.WriteLine("Rightmost node is " + max);
}
  
// Driver Code
public static void Main(String[] args) 
{
        int []preorder = { 3, 2, 1, 5, 4 };
        int n = 5;
        LeftRightNode(preorder, n);
  
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

Leftmost node is 1
Rightmost node is 5

Time Complexity: O(N)
Space Complexity: O(1)



My Personal Notes arrow_drop_up

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.