Check if an array represents Inorder of Binary Search tree or not
Given an array of n elements. The task is to check if it is an Inorder traversal of any Binary Search Tree or not.
Examples:
Input: arr[] = { 19, 23, 25, 30, 45 }
Output: true
Explanation: As the array is sorted in non-decreasing order, it is an Inorder traversal of any Binary Search Tree.
Input : arr[] = { 19, 23, 30, 25, 45 }
Output : false
Explanation: As the array is not sorted in non-decreasing order, it is not an Inorder traversal of any Binary Search Tree.
Approach:
The idea is to use the fact that the in-order traversal of Binary Search Tree is sorted. So, just check if given array is sorted or not.
Below is the implementation of the above approach:
// C++ program to check if a given array is sorted
// or not.
#include<bits/stdc++.h>
using namespace std;
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
bool isInorder(vector<int> &arr) {
int n = arr.size();
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i-1] > arr[i])
return false;
// No unsorted pair found
return true;
}
int main() {
vector<int> arr = { 19, 23, 25, 30, 45 };
if (isInorder(arr))
cout << "true";
else
cout << "false";
return 0;
}
// C program to check if a given array is sorted
// or not.
#include <stdio.h>
#include <stdbool.h>
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
bool isInorder(int arr[], int n) {
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++) {
// Unsorted pair found
if (arr[i-1] > arr[i])
return false;
}
// No unsorted pair found
return true;
}
int main() {
int arr[] = { 19, 23, 25, 30, 45 };
int n = sizeof(arr) / sizeof(arr[0]);
if (isInorder(arr, n))
printf("true");
else
printf("false");
return 0;
}
// Java program to check if a given array is sorted
// or not.
import java.util.ArrayList;
class GfG {
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
static boolean isInorder(ArrayList<Integer> arr) {
int n = arr.size();
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++) {
// Unsorted pair found
if (arr.get(i - 1) > arr.get(i))
return false;
}
// No unsorted pair found
return true;
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add(19);
arr.add(23);
arr.add(25);
arr.add(30);
arr.add(45);
if (isInorder(arr))
System.out.println("true");
else
System.out.println("false");
}
}
# Python program to check if a given array is sorted
# or not.
# Function that returns true if array is Inorder
# traversal of any Binary Search Tree or not.
def isInorder(arr):
n = len(arr)
# Array has one or no element
if n == 0 or n == 1:
return True
for i in range(1, n):
# Unsorted pair found
if arr[i-1] > arr[i]:
return False
# No unsorted pair found
return True
if __name__ == "__main__":
arr = [19, 23, 25, 30, 45]
if isInorder(arr):
print("true")
else:
print("false")
// C# program to check if a given array is sorted
// or not.
using System;
using System.Collections.Generic;
class GfG {
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
static bool isInorder(List<int> arr) {
int n = arr.Count;
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++) {
// Unsorted pair found
if (arr[i-1] > arr[i])
return false;
}
// No unsorted pair found
return true;
}
static void Main() {
List<int> arr = new List<int> { 19, 23, 25, 30, 45 };
if (isInorder(arr))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
// JavaScript program to check if a given array is sorted
// or not.
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
function isInorder(arr) {
let n = arr.length;
// Array has one or no element
if (n === 0 || n === 1)
return true;
for (let i = 1; i < n; i++) {
// Unsorted pair found
if (arr[i-1] > arr[i])
return false;
}
// No unsorted pair found
return true;
}
const arr = [19, 23, 25, 30, 45];
if (isInorder(arr))
console.log("true");
else
console.log("false");
Output
true
Time complexity: O(n) where n is the size of array.
Auxiliary Space: O(1)