Check if an array represents Inorder of Binary Search tree or not - GeeksforGeeks

25 May, 2021
Given an array of N element. The task is to check if it is Inorder traversal of any Binary Search Tree or not. Print “Yes” if it is Inorder traversal of any Binary Search Tree else print “No”. Examples:  
Input : arr[] = { 19, 23, 25, 30, 45 }
Output : Yes

Input : arr[] = { 19, 23, 30, 25, 45 }
Output : No
 
The idea is to use the fact that the inorder traversal of Binary Search Tree is sorted. So, just check if given array is sorted or not.  
C++
Java
Python3
C#
PHP
Javascript
// 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(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;} // Driver codeint main(){    int arr[] = { 19, 23, 25, 30, 45 };    int n = sizeof(arr)/sizeof(arr[0]);         if (isInorder(arr, n))        cout << "Yesn";    else        cout << "Non";           return 0;}
// Java program to check if a given array is sorted// or not. class GFG { // Function that returns true if array is Inorder// traversal of any Binary Search Tree or not.    static boolean 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;    }// Drivers code     public static void main(String[] args) {        int arr[] = {19, 23, 25, 30, 45};        int n = arr.length;        if (isInorder(arr, n)) {            System.out.println("Yes");        } else {            System.out.println("Non");        }    }}//This code is contributed by 29AjayKumar
# Python 3 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):         # Array has one or no element    if (n == 0 or n == 1):        return True     for i in range(1, n, 1):                 # Unsorted pair found        if (arr[i - 1] > arr[i]):            return False     # No unsorted pair found    return True # Driver codeif __name__ == '__main__':    arr = [19, 23, 25, 30, 45]    n = len(arr)         if (isInorder(arr, n)):        print("Yes")    else:        print("No")         # This code is contributed by# Sahil_Shelangia
// C# program to check if a given// array is sorted or not.using System; class GFG{ // Function that returns true if// array is Inorder traversal of// any Binary Search Tree or not.static bool isInorder(int[] arr, int n){    // Array has one or no element    if (n == 0 || n == 1)    {        return true;    }         // Unsorted pair found    for (int i = 1; i < n; i++)    {        if (arr[i - 1] > arr[i])        {            return false;        }    }     // No unsorted pair found    return true;} // Driver codepublic static void Main(){    int []arr = {19, 23, 25, 30, 45};    int n = arr.Length;    if (isInorder(arr, n))    {        Console.Write("Yes");    }    else    {        Console.Write("Non");    }}} // This code is contributed by Rajput-Ji
<?php// PHP 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, $n){    // Array has one or no element    if ($n == 0 || $n == 1)        return true;     for ($i = 1; $i < $n; $i++)         // Unsorted pair found        if ($arr[$i - 1] > $arr[$i])            return false;     // No unsorted pair found    return true;} // Driver code$arr = array(19, 23, 25, 30, 45);$n = sizeof($arr); if (isInorder($arr, $n))    echo "Yes";else    echo "No"; // This code is contributed// by Akanksha Rai?>
<script>// 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 , n)    {             // Array has one or no element        if (n == 0 || n == 1) {            return true;        }         for (i = 1; i < n; i++) // Unsorted pair found        {            if (arr[i - 1] > arr[i]) {                return false;            }        }         // No unsorted pair found        return true;    }         // Drivers code        var arr = [ 19, 23, 25, 30, 45 ];        var n = arr.length;        if (isInorder(arr, n)) {            document.write("Yes");        } else {            document.write("Non");        } // This code is contributed by Rajput-Ji</script>
Output:  
Yes
 
YouTubeGeeksforGeeks500K subscribersCheck if an array represents Inorder of Binary Search tree or not | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:32•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=v3ADo5kpVSE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
29AjayKumar
Rajput-Ji
Akanksha_Rai
sahilshelangia
Binary Search Tree
Tree
Binary Search Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Advantages of BST over Hash Table
Difference between Binary Tree and Binary Search Tree
Binary Tree to Binary Search Tree Conversion
Insert a node in Binary Search Tree Iteratively
Sorted Linked List to Balanced BST
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
Inorder Tree Traversal without Recursion
Binary Tree | Set 3 (Types of Binary Tree)