Check if an array can be Preorder of a BST
Last Updated :
20 Nov, 2024
Given an array of numbers, the task is to check if the given array can represent the preorder traversal of a Binary Search Tree.
Examples:
Input: pre[] = [40, 30, 35, 80, 100]
Output: true
Explanation: Given array can represent preorder traversal of below tree
Input: pre[] = [2, 4, 1]
Output: false
Explanation: Given array cannot represent preorder traversal of a Binary Search Tree.
[Naive approach] By find next element - O(n^2) Time and O(n) Space
The idea is to search for next greater element for every pre[i] starting from first one. Here below steps that can be follow for this approach:
- Find the first greater value on right side of current node. Let the index of this node be j.
- Return true if following conditions hold. Else return false.
- All values after the above found greater value are greater than current node.
- Recursive calls for the subarrays pre[i+1..j-1] and pre[j+1..n-1] also return true.
The Time complexity for this approach is O(n ^ 2) where n is the number of elements in pre order.
[Expected Approach - 1] Using Stack - O(n) Time and O(n) Space
The idea is to use a stack. This problem is similar to Next (or closest) Greater Element problem. Here we find the next greater element and after finding next greater, if we find a smaller element, then return false.
Follow the steps below to solve the problem:
- Start with the smallest possible value as the root and an empty stack.
- Iterate through the array. If the current element is smaller than the root, return
false
(violates the BST property). - For each element:
- If it is smaller than the stack's top, it belongs to the left subtree. Push it onto the stack.
- If it is larger, it belongs to the right subtree. Pop elements from the stack (moving up the tree) until the top is larger than the current element, updating the root with the last popped value. Push the current element onto the stack.
- If the loop completes without violations, the array represents a valid BST preorder traversal.
C++
// C++ program to check if given array can represent Preorder
// traversal of a Binary Search Tree
#include<bits/stdc++.h>
using namespace std;
bool canRepresentBST(vector<int> &pre) {
stack<int> s;
// Initialize current root as minimum possible
// value
int root = INT_MIN;
for (int i = 0; i < pre.size(); i++) {
// If we find a node who is on right side
// and smaller than root, return false
if (pre[i] < root)
return false;
// If pre[i] is in right subtree of stack top,
// Keep removing items smaller than pre[i]
// and make the last removed item as new
// root.
while (!s.empty() && s.top() < pre[i]) {
root = s.top();
s.pop();
}
// At this point either stack is empty or
// pre[i] is smaller than root, push pre[i]
s.push(pre[i]);
}
return true;
}
int main() {
vector<int> pre = {40, 30, 35, 80, 100};
if (canRepresentBST(pre))
cout << "true\n";
else
cout << "false\n";
return 0;
}
Java
// Java program to check if given array can represent Preorder
// traversal of a Binary Search Tree
import java.util.*;
class GfG {
static boolean canRepresentBST(List<Integer> pre) {
Stack<Integer> s = new Stack<>();
// Initialize current root as minimum possible
// value
int root = Integer.MIN_VALUE;
for (int i = 0; i < pre.size(); i++) {
// If we find a node who is on right side
// and smaller than root, return false
if (pre.get(i) < root)
return false;
// If pre[i] is in right subtree of stack top,
// Keep removing items smaller than pre[i]
// and make the last removed item as new
// root.
while (!s.isEmpty() && s.peek() < pre.get(i)) {
root = s.pop();
}
// At this point either stack is empty or
// pre[i] is smaller than root, push pre[i]
s.push(pre.get(i));
}
return true;
}
public static void main(String[] args) {
List<Integer> pre = Arrays.asList(40, 30, 35, 80, 100);
if (canRepresentBST(pre))
System.out.println("true");
else
System.out.println("false");
}
}
Python
# Python program to check if given array can represent Preorder
# traversal of a Binary Search Tree
def canRepresentBST(pre):
s = []
# Initialize current root as minimum possible
# value
root = float('-inf')
for value in pre:
# If we find a node who is on right side
# and smaller than root, return false
if value < root:
return False
# If pre[i] is in right subtree of stack top,
# Keep removing items smaller than pre[i]
# and make the last removed item as new
# root.
while s and s[-1] < value:
root = s.pop()
# At this point either stack is empty or
# pre[i] is smaller than root, push pre[i]
s.append(value)
return True
if __name__ == "__main__":
pre = [40, 30, 35, 80, 100]
if canRepresentBST(pre):
print("true")
else:
print("false")
C#
// C# program to check if given array can represent Preorder
// traversal of a Binary Search Tree
using System;
using System.Collections.Generic;
class GfG {
static bool canRepresentBST(List<int> pre) {
Stack<int> s = new Stack<int>();
// Initialize current root as minimum possible
// value
int root = int.MinValue;
for (int i = 0; i < pre.Count; i++) {
// If we find a node who is on right side
// and smaller than root, return false
if (pre[i] < root)
return false;
// If pre[i] is in right subtree of stack top,
// Keep removing items smaller than pre[i]
// and make the last removed item as new
// root.
while (s.Count > 0 && s.Peek() < pre[i]) {
root = s.Pop();
}
// At this point either stack is empty or
// pre[i] is smaller than root, push pre[i]
s.Push(pre[i]);
}
return true;
}
static void Main() {
List<int> pre = new List<int>() { 40, 30, 35, 80, 100 };
if (canRepresentBST(pre))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
// Javascript program to check if given array can
// represent Preorder traversal of a Binary Search Tree
function canRepresentBST(pre) {
let s = [];
// Initialize current root as minimum possible
// value
let root = -Infinity;
for (let i = 0; i < pre.length; i++) {
// If we find a node who is on right side
// and smaller than root, return false
if (pre[i] < root)
return false;
// If pre[i] is in right subtree of stack top,
// Keep removing items smaller than pre[i]
// and make the last removed item as new
// root.
while (s.length > 0 && s[s.length - 1] < pre[i]) {
root = s.pop();
}
// At this point either stack is empty or
// pre[i] is smaller than root, push pre[i]
s.push(pre[i]);
}
return true;
}
let pre = [40, 30, 35, 80, 100];
if (canRepresentBST(pre))
console.log("true");
else
console.log("false");
[Expected Approach - 2] Without Using Stack - O(n) Time and O(n) Space
We can check if the given preorder traversal is valid or not for a BST without using stack. The idea is to use the similar concept of Building a BST using narrowing bound algorithm. We will recursively visit all nodes, but we will not build the nodes. In the end, if the complete array is not traversed, then that means that array can not represent the preorder traversal of any binary tree.
C++
// C++ program to check if a given array can represent
// a preorder traversal of a BST or not
#include <bits/stdc++.h>
using namespace std;
void buildBSThelper(int& preIndex, int n, vector<int> &pre,
int min, int max) {
// If we have processed all elements, return
if (preIndex >= n)
return;
// If the current element lies between min and max,
// it can be part of the BST
if (min <= pre[preIndex] && pre[preIndex] <= max) {
// Treat the current element as the root of
// this subtree
int rootData = pre[preIndex];
preIndex++;
buildBSThelper(preIndex, n, pre, min, rootData);
buildBSThelper(preIndex, n, pre, rootData, max);
}
}
bool canRepresentBST(vector<int> &arr) {
// Set the initial min and max values
int min = INT_MIN, max = INT_MAX;
// Start from the first element in
// the array
int preIndex = 0;
int n = arr.size();
buildBSThelper(preIndex, n, arr, min, max);
// If all elements are processed, it means the
// array represents a valid BST
return preIndex == n;
}
int main() {
vector<int> pre = {40, 30, 35, 80, 100};
if (canRepresentBST(pre))
cout << "true\n";
else
cout << "false\n";
return 0;
}
Java
// Java program to check if a given array can represent
// a preorder traversal of a BST or not
import java.util.*;
class GfG {
static void buildBSThelper(int[] preIndex, int n, List<Integer> pre,
int min, int max) {
// If we have processed all elements, return
if (preIndex[0] >= n)
return;
// If the current element lies between min and max,
// it can be part of the BST
if (min <= pre.get(preIndex[0]) && pre.get(preIndex[0]) <= max) {
// Treat the current element as the root of this subtree
int rootData = pre.get(preIndex[0]);
preIndex[0]++;
buildBSThelper(preIndex, n, pre, min, rootData);
buildBSThelper(preIndex, n, pre, rootData, max);
}
}
// Function to check if the array can represent a
// valid BST
static boolean canRepresentBST(List<Integer> arr) {
// Set the initial min and max values
int min = Integer.MIN_VALUE, max = Integer.MAX_VALUE;
// Start from the first element in the array
int[] preIndex = {0};
int n = arr.size();
buildBSThelper(preIndex, n, arr, min, max);
// If all elements are processed, it means the
// array represents a valid BST
return preIndex[0] == n;
}
public static void main(String[] args) {
List<Integer> pre = Arrays.asList(40, 30, 35, 80, 100);
if (canRepresentBST(pre))
System.out.println("true");
else
System.out.println("false");
}
}
Python
# Python program to check if a given array can represent
# a preorder traversal of a BST or not
def buildBST_helper(preIndex, n, pre, min_val, max_val):
# If we have processed all elements,
# return
if preIndex[0] >= n:
return
# If the current element lies between
# min and max,
# it can be part of the BST
if min_val <= pre[preIndex[0]] <= max_val:
# Treat the current element as the root of
# this subtree
rootData = pre[preIndex[0]]
preIndex[0] += 1
buildBST_helper(preIndex, n, pre, min_val, rootData)
buildBST_helper(preIndex, n, pre, rootData, max_val)
def canRepresentBST(arr):
# Set the initial min and max values
min_val = float('-inf')
max_val = float('inf')
# Start from the first element
# in the array
preIndex = [0]
n = len(arr)
buildBST_helper(preIndex, n, arr, min_val, max_val)
# If all elements are processed, it means the
# array represents a valid BST
return preIndex[0] == n
pre = [40, 30, 35, 80, 100]
if canRepresentBST(pre):
print("true")
else:
print("false")
C#
// C# program to check if a given array can represent
// a preorder traversal of a BST or not
using System;
using System.Collections.Generic;
class GfG {
static void buildBSThelper(ref int preIndex, int n, List<int> pre,
int min, int max) {
// If we have processed all elements, return
if (preIndex >= n)
return;
// If the current element lies between min and max,
// it can be part of the BST
if (min <= pre[preIndex] && pre[preIndex] <= max) {
// Treat the current element as the root
// of this subtree
int rootData = pre[preIndex];
preIndex++;
buildBSThelper(ref preIndex, n, pre, min, rootData);
buildBSThelper(ref preIndex, n, pre, rootData, max);
}
}
// Function to check if the array can represent a
// valid BST
static bool canRepresentBST(List<int> arr) {
// Set the initial min and max values
int min = int.MinValue, max = int.MaxValue;
// Start from the first element in the array
int preIndex = 0;
int n = arr.Count;
buildBSThelper(ref preIndex, n, arr, min, max);
// If all elements are processed, it means the
// array represents a valid BST
return preIndex == n;
}
static void Main(string[] args) {
List<int> pre = new List<int> {40, 30, 35, 80, 100};
if (canRepresentBST(pre))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
// Javascript program to check if a given array can represent
// a preorder traversal of a BST or not
function buildBSThelper(preIndex, n, pre, min, max) {
// If we have processed all elements, return
if (preIndex[0] >= n)
return;
// If the current element lies between min and max,
// it can be part of the BST
if (min <= pre[preIndex[0]] && pre[preIndex[0]] <= max) {
// Treat the current element as the root of
// this subtree
let rootData = pre[preIndex[0]];
preIndex[0]++;
buildBSThelper(preIndex, n, pre, min, rootData);
buildBSThelper(preIndex, n, pre, rootData, max);
}
}
// Function to check if the array can represent a valid BST
function canRepresentBST(arr) {
// Set the initial min and max values
let min = -Infinity, max = Infinity;
// Start from the first element in the array
let preIndex = [0];
let n = arr.length;
buildBSThelper(preIndex, n, arr, min, max);
// If all elements are processed, it means the
// array represents a valid BST
return preIndex[0] == n;
}
let pre = [40, 30, 35, 80, 100];
if (canRepresentBST(pre))
console.log("true");
else
console.log("false");
Similar Reads
Check whether we can sort two arrays by swapping A[i] and B[i] Given two arrays, we have to check whether we can sort two arrays in strictly ascending order by swapping A[i] and B[i]. Examples: Input : A[ ]={ 1, 4, 3, 5, 7}, B[ ]={ 2, 2, 5, 8, 9} Output : True After swapping A[1] and B[1], both the arrays are sorted. Input : A[ ]={ 1, 4, 5, 5, 7}, B[ ]={ 2, 2,
12 min read
Check if a Binary Tree is BST or not Given the root of a binary tree. Check whether it is a Binary Search Tree or not. A Binary Search Tree (BST) is a node-based binary tree data structure with the following properties. All keys in the left subtree are smaller than the root and all keys in the right subtree are greater.Both the left an
15+ min read
Check if array can be sorted with one swap Given an array containing N elements. Find if it is possible to sort it in non-decreasing order using atmost one swap. Examples: Input : arr[] = {1, 2, 3, 4} Output : YES The array is already sorted Input : arr[] = {3, 2, 1} Output : YES Swap 3 and 1 to get [1, 2, 3] Input : arr[] = {4, 1, 2, 3} Out
11 min read
Check if an array can be Arranged in Left or Right Positioned Array Given an array arr[] of size n>4, the task is to check whether the given array can be arranged in the form of Left or Right positioned array? Left or Right Positioned Array means each element in the array is equal to the number of elements to its left or number of elements to its right. Examples
8 min read
Check if an array is Wave Array Given an array of N positive integers. The task is to check if the array is sorted in wave form. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: NO Input: arr[] = {1, 5, 3, 7, 2, 8, 6}Output: YES Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach: First c
14 min read