Given a preorder traversal of a BST. The task is to find the number of elements less than root.
Examples:
Input: preorder[] = {3, 2, 1, 0, 5, 4, 6}
Output: 3
Input: preorder[] = {5, 4, 3, 2, 1}
Output: 4
For a binary search tree, a preorder traversal is of the form:
root, { elements in left subtree of root }, { elements in right subtree of root }
Simple approach:
- Traverse the given preorder.
- Check if the current element is greater than root.
- If yes then return the indexOfCurrentElement – 1 as the no. elements smaller than root will be all the elements occurs before the currentelement except root.
C++
// C++ implementation of above approach#include <iostream>using namespace std; // Function to find the first index of the element// that is greater than the rootint findLargestIndex(int arr[], int n){ int i, root = arr[0]; // Traverse the given preorder for(i = 0; i < n-1; i++) { // Check if the number is greater than root // If yes then return that index-1 if(arr[i] > root) return i-1; } } // Driver Codeint main(){ int preorder[] = {3, 2, 1, 0, 5, 4, 6}; int n = sizeof(preorder) / sizeof(preorder[0]); cout << findLargestIndex(preorder, n); return 0;} |
Java
// Java implementation of// above approach class GFG{// Function to find the first // index of the element that // is greater than the rootstatic int findLargestIndex(int arr[], int n){ int i, root = arr[0]; // Traverse the given preorder for(i = 0; i < n - 1; i++) { // Check if the number is // greater than root // If yes then return // that index-1 if(arr[i] > root) return i-1; } return 0;} // Driver Codepublic static void main(String ags[]){ int preorder[] = {3, 2, 1, 0, 5, 4, 6}; int n = preorder.length; System.out.println(findLargestIndex(preorder, n));}} // This code is contributed // by Subhadeep Gupta |
Python3
# Python3 implementation of above approach # Function to find the first index of # the element that is greater than the rootdef findLargestIndex(arr, n): i, root = arr[0], arr[0]; # Traverse the given preorder for i in range(0, n - 1): # Check if the number is greater than # root. If yes then return that index-1 if(arr[i] > root): return i - 1; # Driver Codepreorder= [3, 2, 1, 0, 5, 4, 6];n = len(preorder) print(findLargestIndex(preorder, n)); # This code is contributed # by Akanksha Rai |
C#
// C# implementation of above approachusing System; class GFG{ // Function to find the first // index of the element that// is greater than the rootstatic int findLargestIndex(int []arr, int n){ int i, root = arr[0]; // Traverse the given preorder for(i = 0; i < n - 1; i++) { // Check if the number is // greater than root. If yes // then return that index-1 if(arr[i] > root) return i - 1; } return 0;} // Driver Codestatic public void Main(){ int []preorder = {3, 2, 1, 0, 5, 4, 6}; int n = preorder.Length; Console.WriteLine(findLargestIndex(preorder, n));}} // This code is contributed // by Subhadeep Gupta |
PHP
<?php// PHP implementation of above approach // Function to find the first index of // the element that is greater than the root function findLargestIndex( $arr, $n) { $i; $root = $arr[0]; // Traverse the given preorder for($i = 0; $i < $n - 1; $i++) { // Check if the number is greater than // root. If yes, then return that index-1 if($arr[$i] > $root) return $i - 1; } } // Driver Code $preorder = array(3, 2, 1, 0, 5, 4, 6); $n = count($preorder);echo findLargestIndex($preorder, $n); // This code is contributed // by 29AjayKumar?> |
3
Time complexity: O(n)
Efficient approach (Using Binary Search): Here the idea is to make use of an extended form of binary search. The steps are as follows:
- Go to mid. Check if the element at mid is greater than root. If yes then we recurse on the left half of array.
- Else if the element at mid is lesser than root and element at mid+1 is greater than root we return mid as our answer.
- Else we recurse on the right half of array to repeat the above steps.
Below is the implementation of the above idea.
C++
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to count the smaller elementsint findLargestIndex(int arr[], int n){ int root = arr[0], lb = 0, ub = n-1; while(lb < ub) { int mid = (lb + ub)/2; // Check if the element at mid // is greater than root. if(arr[mid] > root) ub = mid - 1; else { // if the element at mid is lesser // than root and element at mid+1 // is greater if(arr[mid + 1] > root) return mid; else lb = mid + 1; } } return lb;} // Driver Codeint main(){ int preorder[] = {3, 2, 1, 0, 5, 4, 6}; int n = sizeof(preorder) / sizeof(preorder[0]); cout << findLargestIndex(preorder, n); return 0;} |
Java
// Java implementation // of above approachimport java.util.*; class GFG{ // Function to count the// smaller elementsstatic int findLargestIndex(int arr[], int n){ int root = arr[0], lb = 0, ub = n - 1; while(lb < ub) { int mid = (lb + ub) / 2; // Check if the element at // mid is greater than root. if(arr[mid] > root) ub = mid - 1; else { // if the element at mid is // lesser than root and // element at mid+1 is greater if(arr[mid + 1] > root) return mid; else lb = mid + 1; } } return lb;} // Driver Codepublic static void main(String args[]){ int preorder[] = {3, 2, 1, 0, 5, 4, 6}; int n = preorder.length; System.out.println( findLargestIndex(preorder, n));}} // This code is contributed by Arnab Kundu |
Python3
# Python3 implementation of above approach # Function to count the smaller elementsdef findLargestIndex(arr, n): root = arr[0]; lb = 0; ub = n - 1; while(lb < ub): mid = (lb + ub) // 2; # Check if the element at mid # is greater than root. if(arr[mid] > root): ub = mid - 1; else: # if the element at mid is lesser # than root and element at mid+1 # is greater if(arr[mid + 1] > root): return mid; else: lb = mid + 1; return lb; # Driver Codepreorder = [3, 2, 1, 0, 5, 4, 6];n = len(preorder); print(findLargestIndex(preorder, n)); # This code is contributed by mits |
C#
// C# implementation // of above approach using System; class GFG { // Function to count the // smaller elements static int findLargestIndex(int []arr, int n) { int root = arr[0], lb = 0, ub = n - 1; while(lb < ub) { int mid = (lb + ub) / 2; // Check if the element at // mid is greater than root. if(arr[mid] > root) ub = mid - 1; else { // if the element at mid is // lesser than root and // element at mid+1 is greater if(arr[mid + 1] > root) return mid; else lb = mid + 1; } } return lb; } // Driver Code public static void Main(String []args) { int []preorder = {3, 2, 1, 0, 5, 4, 6}; int n = preorder.Length; Console.WriteLine( findLargestIndex(preorder, n)); } } // This code contributed by Rajput-Ji |
PHP
<?php// PHP implementation of above approach // Function to count the smaller elementsfunction findLargestIndex($arr, $n){ $root = $arr[0]; $lb = 0; $ub = $n - 1; while($lb < $ub) { $mid = (int)(($lb + $ub) / 2); // Check if the element at mid // is greater than root. if($arr[$mid] > $root) $ub = $mid - 1; else { // if the element at mid is lesser // than root and element at mid+1 // is greater if($arr[$mid + 1] > $root) return $mid; else $lb = $mid + 1; } } return $lb;} // Driver Code$preorder = array(3, 2, 1, 0, 5, 4, 6);$n = count($preorder); echo findLargestIndex($preorder, $n); // This code is contributed by mits?> |
3
Time Complexity: O(logn)
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.



