Check if an array represents Inorder of Binary Search tree or not
Last Updated : 5 May, 2026
Given an array arr[] of size n containing unique elements, check whether it represents the inorder traversal of a Binary Search Tree (BST) or not.
Examples:
Input: arr[] = { 19, 23, 25, 30, 45 } Output: true Explaination: Given array is inorder traversal for the following tree:
Input : arr[] = { 19, 23, 30, 25, 45 } Output : false Explanation: The array is not in increasing order (since 30 > 25), so it cannot represent the inorder traversal of a Binary Search Tree (BST).
The idea is to use the fundamental property of BST: Inorder traversal of a Binary Search Tree always gives a sorted sequence. So, simply check whether the given array is sorted in increasing order or not.
C++
#include<bits/stdc++.h>usingnamespacestd;// Function that returns true if array is Inorder// traversal of any Binary Search Tree or not.booldoesRepresentBST(vector<int>&arr){intn=arr.size();// Array has one or no elementif(n==0||n==1)returntrue;for(inti=1;i<n;i++)// Unsorted pair foundif(arr[i-1]>arr[i])returnfalse;// No unsorted pair foundreturntrue;}intmain(){vector<int>arr={19,23,25,30,45};if(doesRepresentBST(arr))cout<<"true";elsecout<<"false";return0;}
C
#include<stdio.h>#include<stdbool.h>// Function that returns true if array is Inorder// traversal of any Binary Search Tree or not.booldoesRepresentBST(intarr[],intn){// Array has one or no elementif(n==0||n==1)returntrue;for(inti=1;i<n;i++){// Unsorted pair foundif(arr[i-1]>arr[i])returnfalse;}// No unsorted pair foundreturntrue;}intmain(){intarr[]={19,23,25,30,45};intn=sizeof(arr)/sizeof(arr[0]);if(doesRepresentBST(arr,n))printf("true");elseprintf("false");return0;}
Java
importjava.util.ArrayList;classGfG{// Function that returns true if array is Inorder// traversal of any Binary Search Tree or not.staticbooleandoesRepresentBST(ArrayList<Integer>arr){intn=arr.size();// Array has one or no elementif(n==0||n==1)returntrue;for(inti=1;i<n;i++){// Unsorted pair foundif(arr.get(i-1)>arr.get(i))returnfalse;}// No unsorted pair foundreturntrue;}publicstaticvoidmain(String[]args){ArrayList<Integer>arr=newArrayList<>();arr.add(19);arr.add(23);arr.add(25);arr.add(30);arr.add(45);if(doesRepresentBST(arr))System.out.println("true");elseSystem.out.println("false");}}
Python
# Function that returns true if array is Inorder# traversal of any Binary Search Tree or not.defdoesRepresentBST(arr):n=len(arr)# Array has one or no elementifn==0orn==1:returnTrueforiinrange(1,n):# Unsorted pair foundifarr[i-1]>arr[i]:returnFalse# No unsorted pair foundreturnTrueif__name__=="__main__":arr=[19,23,25,30,45]ifdoesRepresentBST(arr):print("true")else:print("false")
C#
usingSystem;usingSystem.Collections.Generic;classGfG{// Function that returns true if array is Inorder// traversal of any Binary Search Tree or not.staticbooldoesRepresentBST(List<int>arr){intn=arr.Count;// Array has one or no elementif(n==0||n==1)returntrue;for(inti=1;i<n;i++){// Unsorted pair foundif(arr[i-1]>arr[i])returnfalse;}// No unsorted pair foundreturntrue;}staticvoidMain(){List<int>arr=newList<int>{19,23,25,30,45};if(doesRepresentBST(arr))Console.WriteLine("true");elseConsole.WriteLine("false");}}
JavaScript
// Function that returns true if array is Inorder// traversal of any Binary Search Tree or not.functiondoesRepresentBST(arr){letn=arr.length;// Array has one or no elementif(n===0||n===1)returntrue;for(leti=1;i<n;i++){// Unsorted pair foundif(arr[i-1]>arr[i])returnfalse;}// No unsorted pair foundreturntrue;}constarr=[19,23,25,30,45];if(doesRepresentBST(arr))console.log("true");elseconsole.log("false");