255 Verify Preorder Sequence in Binary Search Tree
1. Question
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
You may assume each number in the sequence is unique.
Consider the following binary search tree:
Example 1:
Example 2:
Follow up: Could you do it using only constant space complexity?
2. Implementation
(1) Method 1
思路: preorder 是一个先递减再递增的顺序,维护一个单调递减栈,当前num如果大于栈顶数时,则更新min,如果当前num小于min则该序列并不是preorder序列
3. Time & Space Complexity
Method 1: 时间复杂度 O(n), 空间复杂度O(n)
Last updated
Was this helpful?