Check if an array represents Inorder of Binary Search tree or not Last Updated : 27 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice 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: trueExplanation: 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 : falseExplanation: 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++ // 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 // 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 // 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 # 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# // 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 // 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"); OutputtrueTime complexity: O(n) where n is the size of array.Auxiliary Space: O(1) Inorder Traversal and BST | DSA Problem Comment More infoAdvertise with us Next Article Introduction to Binary Search Tree K kartik Improve Article Tags : Tree Binary Search Tree DSA Practice Tags : Binary Search TreeTree Similar Reads Binary Search Tree A Binary Search Tree (BST) is a type of binary tree data structure in which each node contains a unique key and satisfies a specific ordering property:All nodes in the left subtree of a node contain values strictly less than the nodeâs value. All nodes in the right subtree of a node contain values s 4 min read Introduction to Binary Search Tree Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the 3 min read Applications of BST Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward.The left subtree of a node contains only node 3 min read Applications, Advantages and Disadvantages of Binary Search Tree A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p 2 min read Insertion in Binary Search Tree (BST) Given a BST, the task is to insert a new node in this BST.Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is fo 15 min read Searching in Binary Search Tree (BST) Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp 7 min read Deletion in Binary Search Tree (BST) Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios:Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BSTDeleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node w 10 min read Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1 10 min read Balance a Binary Search Tree Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height.Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height.Input: Output: Explanation: The above u 10 min read Self-Balancing Binary Search Trees Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree. The height is typically maintained in order of logN so that all operations take O(logN) time on average 4 min read Like