How many traversals need to be known to construct a BST
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
For a general binary tree, one traversal is not enough to reconstruct the tree uniquely. But for a binary search tree with unique keys, the answer is different: a single preorder traversal or a single postorder traversal is enough to reconstruct the BST. The BST ordering rule carries extra structure, so you do not need the same amount of information that a general binary tree requires.
Why a BST Is Special
A BST has the property that:
- every value in the left subtree is smaller than the root
- every value in the right subtree is larger than the root
That ordering constraint dramatically reduces ambiguity.
For an arbitrary binary tree, preorder alone does not tell you where the left subtree ends and the right subtree begins. In a BST, the value ranges determine those boundaries.
One Preorder Traversal Is Enough
Suppose the preorder traversal is:
The first value, 10, is the root. In a BST preorder sequence, values less than 10 belong to the left subtree and values greater than 10 belong to the right subtree.
So:
- left subtree preorder:
5 1 7 - right subtree preorder:
40 50
You can then recurse on each side.
Example Construction From Preorder
A simple Python implementation uses range bounds.
This works because every recursive call knows the legal value range for that subtree.
One Postorder Traversal Is Also Enough
The same idea works with postorder if you process the sequence from the end. In postorder, the last element is the root, and the BST bounds again tell you how to split the remaining values.
So for unique keys:
- preorder alone is sufficient
- postorder alone is sufficient
Inorder alone is not sufficient, because the inorder traversal of a BST is just the sorted key list. Many different BST shapes can produce the same sorted order.
What About Two Traversals?
If someone learned that you need inorder plus preorder or inorder plus postorder to reconstruct a tree, that statement applies to general binary trees. It is true there, but it is stronger than necessary for a BST.
That is the key distinction:
- general binary tree: usually need two traversals, one of them inorder
- BST with unique keys: one preorder or one postorder traversal is enough
Common Pitfalls
- Applying the rule for general binary trees directly to BSTs.
- Thinking inorder alone is enough because BST inorder is sorted.
- Forgetting that uniqueness of keys matters for unambiguous reconstruction.
- Missing the fact that preorder and postorder work because BST ordering supplies subtree boundaries.
- Assuming level-order is required when preorder already contains enough information.
Summary
- For a BST with unique keys, one preorder traversal is enough to reconstruct it.
- One postorder traversal is also enough.
- Inorder alone is not enough because it only gives sorted values.
- The BST ordering property provides structural information that general binary trees do not have.
- The "two traversals required" rule applies to general binary trees, not specifically to BSTs.
Related reading
- How many ways can you insert a series of values into a BST to form a specific tree?
- How much time does it take to train a SVM classifier?
- How OVE is equal to Obd In BFS
- how raft achieve strong consistency when they don't require fsync on every write
- how Message Queue System Works?
- How might I find the largest number contained in a JavaScript array?
- how raft follower rejoin after network disconnected?
- How to account for clock offsets in a distributed system?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.