Check if an array can be Preorder of a BST (original) (raw)

Last Updated : 23 Jul, 2025

Given an **array of numbers, the task is to check if the given array can representthe **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

Check-if-a-given-array-can-represent-Preorder-Traversal-of-Binary-Search-Tree

**Input: pre[] = [2, 4, 1]
**Output: false
**Explanation: Given array cannot represent preorder traversal of a Binary Search Tree.

Try It Yourselfredirect icon

Table of Content

[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:

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:

// 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 &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 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 &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 &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");

`