Checking if given preorder traversal is valid BST
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Binary Search Trees (BSTs) are a fundamental data structure in computer science and often feature in technical interviews and competitive programming. One of the classic problems associated with BSTs is verifying whether a given preorder traversal sequence can represent a valid BST. Understanding this validation can deepen one's grasp of tree structures and their traversal methods.
Preorder Traversal and BST Properties
Recall that preorder traversal of a binary tree visits nodes in the following order:
- Visit the root node.
- Traverse the left subtree.
- Traverse the right subtree.
A Binary Search Tree has the following properties:
- Each node has a value.
- The left subtree of a node contains only nodes with values less than the node's value.
- The right subtree of a node contains only nodes with values greater than the node’s value.
Given a sequence of numbers representing a preorder traversal, the task is to verify if this sequence could have come from a valid BST.
Approach to Verify Preorder Traversal
Method 1: Using a Stack
A practical approach to checking the validity of preorder traversal involves using a stack and keeping track of a lower bound for the current node values.
- Initialization:
Start with an empty stack and a variablelower_boundinitialized to negative infinity. - Iterate through the sequence:
- For each number in the sequence:
- Check the lower bound: If the number is less than the
lower_bound, the sequence cannot represent a valid BST; hence return false. - Update the stack: Pop elements from the stack while the stack’s top is less than the current number. The last popped element becomes the new
lower_bound. - Push the number: Add the current number to the stack.
- Return true: If the entire sequence is processed without violating any conditions, the sequence is a valid preorder traversal of a BST.
Example
Consider the sequence [8, 5, 1, 7, 10, 12].
- Start:
stack = [],lower_bound = -∞. - Process number 8: 8 > -∞,
stack = [8]. - Process number 5: 5 > -∞,
stack = [8, 5]. - Process number 1: 1 > -∞,
stack = [8, 5, 1]. - Process number 7: 7 > 1, pop 1, pop 5,
lower_bound = 5,stack = [8, 7]. - Process number 10: 10 > 7, pop 7, pop 8,
lower_bound = 8,stack = [10]. - Process number 12: 12 > 10,
stack = [10, 12].
No condition for BST is violated; hence, the sequence can represent a valid BST.
Complexity
The time complexity of this method is , where is the length of the sequence. The space complexity is also , owing to the possible storage of all sequence numbers in the stack.
Summary Table
| Step | Action | Stack Content | Lower Bound |
| 1 | Start with an empty stack and lower_bound = -∞. | [] | -∞ |
| 2 | For each number, check it against lower_bound. | ||
| 3 | Pop from stack until the stack top is greater than the current number. | Varies | Updated |
| 4 | Push the number onto the stack. | Varies | |
| 5 | Return true if sequence completes else return false on violation. |
Additional Insights
Method 2: Divide and Conquer
An alternative approach leverages the divide-and-conquer strategy by recursively dividing the sequence based on root values.
- Algorithm:
- Pick the first element as root.
- Find the partition point where all elements after this point are greater than the root.
- Recursively check if the left and right subsequences are valid.
This method provides deeper insights into tree structures but may be less efficient than the stack-based approach due to potential redundant processing of elements.
Edge Cases
- Single element: Always a valid BST.
- Sorted Array: Valid as long as strictly increasing or decreasing.
- Duplicates: Typical BSTs do not allow duplicates; if duplicates are present, additional rules must be defined.
Conclusion
Validating a preorder traversal sequence for a possible BST is a problem that can be efficiently solved with a stack-based strategy. This approach not only provides a clear checking mechanism but also aligns closely with the properties of BSTs, making it intuitive and widely applicable. Understanding and implementing these methods develops critical insights into trees and their traversal paradigms, forming a key part of any programmer’s toolkit.

