Skip to main content
LeetCode 255, Medium. Topics: Stack, Tree, Binary Search Tree, Recursion, Array, Binary Tree, Monotonic Stack. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026