Talentd
FleetCode

255. Verify Preorder Sequence in Binary Search Tree

Medium51.7% acceptance5 companiesPremium elsewhere, free here

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

Example 1:

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

Example 2:

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

Constraints:

  • 1 <= preorder.length <= 104
  • 1 <= preorder[i] <= 104
  • All the elements of preorder are unique.

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

11:36
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[5,2,1,3,6]
Input
[5,2,6,1,3]
Your code is saved on this device.Solution