Given an integer N, the task is to count the number of possible Binary Search Trees with N keys.
Input: N = 2 Output: 2 For N = 2, there are 2 unique BSTs 1 2 \ / 2 1 Input: N = 9 Output: 4862
Approach: The number of binary search trees that will be formed with N keys can be calculated by simply evaluating the corresponding number in Catalan Number series.
First few Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …
Catalan numbers satisfy the following recursive formula:
Below is the implementation of the above approach:
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Count the Number of Binary Search Trees present in a Binary Tree
- Number of pairs with a given sum in a Binary Search Tree
- Program for nth Catalan Number
- Number of Binary Trees for given Preorder Sequence length
- Self-Balancing-Binary-Search-Trees (Comparisons)
- Check whether the two Binary Search Trees are Identical or Not
- Merge Two Balanced Binary Search Trees
- Number of full binary trees such that each node is product of its children
- Print Common Nodes in Two Binary Search Trees
- Number of steps required to convert a binary number to one
- Count the total number of squares that can be visited by Bishop in one move
- Binary Search Tree | Set 1 (Search and Insertion)
- Number of trees whose sum of degrees of all the vertices is L
- m-WAY Search Trees | Set-1 ( Searching )
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.