Given a sorted integer array, how may Binary Search trees can be formed from it?
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
When working with a sorted integer array, you might wonder how many unique Binary Search Trees (BSTs) can be constructed from it. The characteristics of a Binary Search Tree — where the left subtree of a node contains only nodes with values less than the node's value, and the right subtree only nodes with values greater — ties deeply into this consideration.
Technical Explanation
For a given sorted integer array, the question of how many distinct BSTs can be formed is solved by understanding Catalan numbers. To figure this out, it's essential to delve into combinatorial structures and dynamic programming.
Catalan Numbers
Catalan numbers provide the precise solution to finding the number of distinct BSTs for a sorted array. The Catalan number can be calculated using the formula:
In terms of BSTs for a sorted array with distinct elements, the number of unique trees that can be formed is equivalently the Catalan number, .
Recursive Approach
To construct a BST from a sorted array, consider choosing any element as the root. Then, the elements to its left form the left subtree, and those to its right form the right subtree. This division is recursive:
- Choose the -th element as the root.
- Form the left subtree with elements from index to .
- Form the right subtree with elements from to the end.
Mathematically, for an array of length :
where represents the number of unique BSTs formed with nodes, and base case . This is the recursive definition of Catalan numbers as it applies to binary trees.
Example
Consider the array `[1, 2, 3]`. The number of unique BSTs that can be formed is given by:
- Choosing 1 as root: • Left subtree: None • Right subtree: Nodes [2, 3]
- Choosing 2 as root: • Left subtree: Node [1] • Right subtree: Node [3]
- Choosing 3 as root: • Left subtree: Nodes [1, 2] • Right subtree: None
Computing this using our recursive approach:
• For root 1: sum of 1 (for left) * 2 (for right) = 2 trees • For root 2: sum of 1 (for left) * 1 (for right) = 1 tree • For root 3: sum of 2 (for left) * 1 (for right) = 2 trees
Thus, total = 2 + 1 + 2 = 5 distinct BSTs.
Computational Complexity
The formula using Catalan numbers allows for efficient computation of the number of trees. The recursion can be implemented using dynamic programming, which runs in for computing values up to . Moreover, since is typically small in practical applications (due to factorial growth), this remains computationally feasible.
Tableau Summary
| Size of Array | Number of BSTs | Example |
| 1 | 1 | [1] |
| 2 | 2 | [1, 2] Trees: 1-2, 2-1 |
| 3 | 5 | [1, 2, 3] |
| 4 | 14 | [1, 2, 3, 4] |
| 5 | 42 | [1, 2, 3, 4, 5] |
Conclusion
The exploration into the number of unique Binary Search Trees (BSTs) that can be formed from a sorted integer array not only provides a fascinating peek into combinatorial mathematics but also underscores the power of Catalan numbers in solving recursive tree-related problems. By employing recursive structures and leveraging dynamic programming, one can efficiently calculate the number of different BSTs that can be constructed from sorted arrays for practical or theoretical applications.
Related reading
- Given a string, find two identical subsequences with consecutive indexes C
- Given a string of a million numbers, return all repeating 3 digit numbers
- Given a word, convert it into a palindrome with minimum addition of letters to it
- Given an array, can I find in On the longest range, whose endpoints are the greatest values in the range?
- Given a tensor flow model graph, how to find the input node and output node names
- Given an array, find out the next smaller element for each element
- Given an array of 0 and 1, find minimum no. of swaps to bring all 1s together only adjacent swaps allowed
- Given an array of integers, find the first missing positive integer in linear time and constant space

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.