0255. Verify Preorder Sequence in Binary Search Tree

https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree

Description

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

Example 1:

Example 2:

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?

ac1: stack, O(n) space

essential idea: right sub-tree always > parent node. very hard to come up with that.

ac2: utilize original array

Key point: using array like a stack.

Last updated

Was this helpful?