> For the complete documentation index, see [llms.txt](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/tree/255.-verify-preorder-sequence-in-binary-search-tree.md).

# 255. Verify Preorder Sequence in Binary Search Tree

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.

You may assume each number in the sequence is unique.

Consider the following binary search tree:&#x20;

```
     5
    / \
   2   6
  / \
 1   3
```

**Example 1:**

```
Input: [5,2,6,1,3]
Output: false
```

**Example 2:**

```
Input: [5,2,1,3,6]
Output: true
```

**Follow up:**\
Could you do it using only constant space complexity?

```cpp
// Stack (simulate preorder traversal)
bool verifyPreorder(vector<int>& preorder) { // time: O(n); space: O(n)
    int low = INT_MIN;
    stack<int> st;
    for (int cur : preorder) {
        if (cur < low) return false;
        while (!st.empty() && cur > st.top()) {
            low = st.top(); st.pop();
        }
        st.push(cur);
    }
    return true;
}
```

```cpp
// Constant space but abuse the input array
bool verifyPreorder(vector<int>& preorder) { // time: O(n); space: O(1)
    int low = INT_MIN, idx = -1;
    for (int cur : preorder) {
        if (cur < low) return false;
        while (idx >= 0 && preorder[idx] < cur) {
            low = preorder[idx--];
        }
        preorder[++idx] = cur;
    }
    return true;
}
```

```cpp
// Divide and Conquer
bool helper(vector<int>& preorder, int start, int end, int lower, int upper) {
    if (start > end) return true;
    int val = preorder[start], i;
    if (val <= lower || val >= upper) return false;
    for (i = start + 1; i <= end; ++i) {
        if (preorder[i] >= val) break;
    }
    return helper(preorder, start + 1, i - 1, lower, val) && helper(preorder, i, end, val, upper);
}
bool verifyPreorder(vector<int>& preorder) { // time: O(n^2); space: O(n)
    return helper(preorder, 0, preorder.size() - 1, INT_MIN, INT_MAX);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/tree/255.-verify-preorder-sequence-in-binary-search-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
