Verify preorder sequence in binary search tree
November 30, 2022
treeProblem URL: Verify preorder sequence in binary search tree
Using stack for storing the current node as a check point for the next element. Once the current is larger than the last element in the stack, we know we should take it as the right node. The last element poping out from the stack will be also a checking point. We will use it to validate the BST property of the current element/node.
class Solution:
def verifyPreorder(self, preorder: List[int]) -> bool:
stack = []
low = -math.inf
for num in preorder:
if num < low:
return False
while stack and num > stack[-1]:
low = stack.pop()
stack.append(num)
return True