> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Verify Preorder Sequence in Binary Search Tree

> Tested Python solution for LeetCode 255 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 255, [Medium](/catalog/medium). Topics: [Stack](/catalog/topics/stack), [Tree](/catalog/topics/tree), [Binary Search Tree](/catalog/topics/binary-search-tree), [Recursion](/catalog/topics/recursion), [Array](/catalog/topics/array), [Binary Tree](/catalog/topics/binary-tree), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 255   # by problem number
lcpy gen -s verify_preorder_sequence_in_binary_search_tree   # by problem name
```

## Problem

Given an array of **unique** integers `preorder`, return `true` *if it is the correct preorder traversal sequence of a binary search tree*.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0255.Verify%20Preorder%20Sequence%20in%20Binary%20Search%20Tree/images/preorder-tree.jpg)

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

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

### Constraints

* `1 <= preorder.length <= 10^4`
* `1 <= preorder[i] <= 10^4`
* All the elements of `preorder` are **unique**.

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/verify_preorder_sequence_in_binary_search_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/verify_preorder_sequence_in_binary_search_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def verify_preorder(self, preorder: list[int]) -> bool:
        stack: list[int] = []
        last_popped = float("-inf")
        for value in preorder:
            if value < last_popped:
                return False
            while stack and stack[-1] < value:
                last_popped = stack.pop()
            stack.append(value)
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.