20. Number of BST From Array
โ GFG solution to the Number of BST From Array problem: count unique BSTs possible with each element as root using Catalan numbers and dynamic programming. ๐
๐งฉ Problem Description
๐ Examples
Example 1
Input: arr[] = [2, 1, 3]
Output: [1, 2, 2]
Explanation:
- When 2 is root: left subtree has 1 element (1), right subtree has 1 element (3)
Number of BSTs = C(1) * C(1) = 1 * 1 = 1
- When 1 is root: left subtree has 0 elements, right subtree has 2 elements (2, 3)
Number of BSTs = C(0) * C(2) = 1 * 2 = 2
- When 3 is root: left subtree has 2 elements (1, 2), right subtree has 0 elements
Number of BSTs = C(2) * C(0) = 2 * 1 = 2Example 2
๐ Constraints
โ
My Approach
Catalan Number with Dynamic Programming
๐ Time and Auxiliary Space Complexity
๐งโ๐ป Code (C++)
โ Code (Java)
๐ Code (Python)
๐ง Contribution and Support
๐Visitor Count
Last updated