Verify Preorder Sequence in Binary Search Tree - Problem

You're given an array of unique integers that represents a potential preorder traversal sequence of a Binary Search Tree (BST). Your task is to determine if this sequence could actually be generated by traversing a valid BST in preorder.

In a preorder traversal, we visit nodes in this order: root โ†’ left subtree โ†’ right subtree. For a valid BST, all values in the left subtree must be smaller than the root, and all values in the right subtree must be larger than the root.

Goal: Return true if the given array represents a valid BST preorder traversal, false otherwise.

Example: For [5,2,1,3,6], this could represent a BST where 5 is root, left subtree contains [2,1,3], and right subtree contains [6]. This follows BST properties, so return true.

Input & Output

example_1.py โ€” Valid BST Preorder
$ Input: [5, 2, 1, 3, 6]
โ€บ Output: true
๐Ÿ’ก Note: This sequence represents a valid BST: root=5, left subtree=[2,1,3] where 2 is root with left=1 and right=3, right subtree=[6]. All BST properties are maintained.
example_2.py โ€” Invalid BST Preorder
$ Input: [5, 2, 6, 1, 3]
โ€บ Output: false
๐Ÿ’ก Note: After visiting 5โ†’2โ†’6, we're in the right subtree of 2. But then we see 1, which is less than 2, violating BST property that right subtree values must be greater than ancestor nodes.
example_3.py โ€” Single Element
$ Input: [1]
โ€บ Output: true
๐Ÿ’ก Note: A single element always forms a valid BST (just the root node).

Visualization

Tap to expand
BST Preorder Validation - Filing System Analogy5ROOT2136Stack Journey:Processing: [5, 2, 1, 3, 6]Visit 5: stack=[5], bound=-โˆžVisit 2: stack=[5,2], bound=-โˆžVisit 1: stack=[5,2,1], bound=-โˆžVisit 3: stack=[5,2,3], bound=1Visit 6: stack=[5,6], bound=2โœ“ Valid BST Preorder!Stack simulation completed without violationsEach value respected the bounds set by its ancestors
Understanding the Visualization
1
Start Journey
Begin at first location (root), can go anywhere initially
2
Track Path
Use stack to remember the path of locations visited
3
Validate Moves
Each new location must respect the 'no going back' rule
4
Update Bounds
When moving right, update minimum allowed for future locations
Key Takeaway
๐ŸŽฏ Key Insight: Use monotonic stack to track the path from root to current position, maintaining bounds to ensure BST property is never violated during preorder traversal.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is pushed and popped from stack at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Stack can contain up to n elements in worst case (all decreasing sequence)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค preorder.length โ‰ค 104
  • 1 โ‰ค preorder[i] โ‰ค 104
  • All integers in preorder are unique
Asked in
Google 28 Amazon 22 Facebook 18 Microsoft 15
28.4K Views
Medium-High Frequency
~18 min Avg. Time
876 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen