Possible permutations of BST's input
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 fixed BST shape, many different insertion orders can produce the same tree. The key rule is that the root must be inserted first, and within the remaining sequence the relative order of nodes in the left subtree and right subtree must each be preserved.
Core Sections
Why multiple insertion orders can lead to the same BST
Suppose the final BST is:
If you insert 3 first, then any valid interleaving of the left-subtree insertion order and the right-subtree insertion order can still reproduce the same final tree, as long as each side keeps its internal order constraints.
For example, these are both valid:
- '
3, 1, 2, 5, 7' - '
3, 5, 1, 7, 2'
Both produce the same BST because all left-subtree values remain less than 3, all right-subtree values remain greater than 3, and the insertion order inside each subtree is still compatible with that subtree's shape.
The recursive counting rule
If a tree has:
- '
Lnodes in the left subtree' - '
Rnodes in the right subtree'
and the number of valid insertion sequences for those subtrees is count(left) and count(right), then the total number of valid sequences for the whole tree is:
The binomial coefficient counts how many ways you can interleave the left and right subtree insertion sequences while preserving each side's internal order.
Example calculation
Consider this BST:
The left subtree has one node and the right subtree has one node.
- '
count(left) = 1' - '
count(right) = 1' - '
choose(2, 1) = 2'
So the valid insertion orders are:
- '
2, 1, 3' - '
2, 3, 1'
That matches the formula exactly.
Recursive implementation in Python
This prints the number of insertion sequences that generate the given BST shape.
Generating the actual sequences
Counting is much easier than listing every valid permutation. To generate the sequences themselves, you recursively generate all valid left sequences and right sequences, then weave them together while preserving internal order.
That is feasible for small trees, but the total number of sequences grows quickly. For anything beyond toy sizes, it is usually better to count them rather than materialize them all.
Common Pitfalls
- Treating the problem like ordinary permutation generation and ignoring the ordering constraints inside each subtree.
- Forgetting that the root must always be first in any insertion sequence that produces the same BST.
- Counting interleavings without multiplying by the valid sequence counts of the left and right subtrees.
- Trying to generate every sequence for large trees when the number of valid orders grows combinatorially.
- Confusing the number of possible BST shapes with the number of insertion orders for one fixed BST.
Summary
- Many insertion orders can produce the same BST, but they must preserve subtree ordering constraints.
- The root value must appear first.
- The total count is
choose(L + R, L) * count(left) * count(right). - Counting valid orders is usually more practical than generating them all.
- The problem is recursive because each subtree has the same structure-and-interleaving logic.
Related reading
- potential On solution to Longest Increasing Subsequence
- Practical Uses of Fractals in Programming
- Pre-order to post-order traversal
- Predicting Values with k-Means Clustering Algorithm
- Postfix notation to expression tree
- Prefix search against half a billion strings
- Predict classes or class probabilities?
- Predicting a Poisson process

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.