Check if an array represents Inorder of Binary Search tree or not
Given an array of N element. The task is to check if it is Inorder traversal of any Binary Search Tree or not.
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.
bool BinarySearchTree::isInorder(vector<int> arr) {
if (arr.empty() || arr.size() == 1) return true;
for (int i=1; i<arr.size(); i++) {
if (arr[i] < arr[i-1]) {
return false;
}
}
return true;
}