|
| 1 | +--- |
| 2 | +id: unique-binary-search-tree |
| 3 | +title: Unique Binary Search Tree |
| 4 | +difficulty: Medium |
| 5 | +sidebar_label: 0096- Unique Binary Search Tree |
| 6 | +tags: |
| 7 | + - Binary Search Tree |
| 8 | + - LeetCode Medium |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem |
| 12 | + |
| 13 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 14 | +| :---------------- | :------------ | :--------------- | |
| 15 | +| [Unique Binary Search Tree ](https://leetcode.com/problems/unique-binary-search-trees/description/) | [Unique Binary Search Tree Solution on LeetCode](https://leetcode.com/problems/unique-binary-search-trees/solutions/4945144/easy-recursion-dynamic-programming-c-solution-beats-100) | [Khushi Kalra](https://leetcode.com/u/abckhush/) | |
| 16 | + |
| 17 | +## Problem Description |
| 18 | +Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n. |
| 19 | + |
| 20 | +### Examples |
| 21 | +**Example 1:** |
| 22 | +```plaintext |
| 23 | +Input: n = 3 |
| 24 | +Output: 5 |
| 25 | +``` |
| 26 | +**Example 2:** |
| 27 | +```plaintext |
| 28 | +Input: n = 1 |
| 29 | +Output: 1 |
| 30 | +``` |
| 31 | + |
| 32 | +### Constraints: |
| 33 | +`1 <= n <= 19` |
| 34 | + |
| 35 | +## Approach#1 : RECURSION |
| 36 | +We use a recursive function numTrees(n) where numTrees(n) returns the number of unique BSTs that can be formed with n nodes. The following steps outline the approach: |
| 37 | + |
| 38 | +1. Base Cases: |
| 39 | +If n is 0 or 1, return 1. There is exactly one unique BST that can be formed with 0 or 1 nodes: |
| 40 | +- numTrees(0) = 1: An empty tree is considered one unique BST. |
| 41 | +- numTrees(1) = 1: A single-node tree is also one unique BST. |
| 42 | + |
| 43 | +2. Recursive Calculation: |
| 44 | +- Initialize count to 0. This variable will accumulate the total number of unique BSTs for n nodes. |
| 45 | +- For each i from 1 to n, consider i as the root of the BST: |
| 46 | + - The left subtree will have i-1 nodes. |
| 47 | + - The right subtree will have n-i nodes. |
| 48 | +- The total number of unique BSTs with i as the root is the product of the number of unique BSTs for the left and right subtrees. |
| 49 | +- Accumulate the results in count by adding the product of the number of unique BSTs for the left and right subtrees: |
| 50 | +`count += numTrees(i-1) * numTrees(n-i)` |
| 51 | + |
| 52 | +3. Return the Result: |
| 53 | +- After computing the total count for all possible roots from 1 to n, return count. |
| 54 | + |
| 55 | +### Complexity |
| 56 | +1. Time complexity: $$O(2^n)$$ |
| 57 | +2. Space complexity: $$O(n)$$ |
| 58 | + |
| 59 | +### Code |
| 60 | +```cpp |
| 61 | +class Solution { |
| 62 | +public: |
| 63 | + int numTrees(int n) { |
| 64 | + if(n==0 || n==1) return 1; |
| 65 | + int count=0; |
| 66 | + for(int i=1; i<=n; i++){ |
| 67 | + count= count + (numTrees(i-1)*numTrees(n-i)); |
| 68 | + } |
| 69 | + return count; |
| 70 | + } |
| 71 | +}; |
| 72 | +``` |
| 73 | +
|
| 74 | +## Approach#2: DYNAMIC PROGRAMMING |
| 75 | +We use a dynamic programming array dp where dp[i] represents the number of unique BSTs that can be formed with i nodes. The following steps outline the approach: |
| 76 | +
|
| 77 | +1. Initialization: |
| 78 | +- Create a dynamic programming array dp of size n+1 initialized with zeros. |
| 79 | +- Set the base cases: |
| 80 | + - dp[0] = 1: An empty tree is considered one unique BST. |
| 81 | + - dp[1] = 1: A single-node tree is also one unique BST. |
| 82 | +
|
| 83 | +2. Filling the DP Array: |
| 84 | +- Iterate over the number of nodes from 2 to n (let's call this i). |
| 85 | +- For each i, consider each node j (from 1 to i) as the root of the BST. |
| 86 | +- The number of unique BSTs with i nodes can be computed as the sum of all possible left and right subtrees: |
| 87 | +- The left subtree will have j-1 nodes. |
| 88 | +- The right subtree will have i-j nodes. |
| 89 | +- Update the dp[i] value as the sum of the products of the number of unique BSTs for the left and right subtrees: |
| 90 | +`dp[i] += dp[j-1] * dp[i-j]` |
| 91 | +
|
| 92 | +3. Return the Result: |
| 93 | +- After filling the dp array, dp[n] will contain the number of unique BSTs that can be formed with n nodes. |
| 94 | +
|
| 95 | +### Complexity |
| 96 | +1. Time complexity: $$O(n^2)$$ |
| 97 | +2. Space complexity: $$O(n)$$ |
| 98 | +
|
| 99 | +### Code |
| 100 | +```cpp |
| 101 | +class Solution { |
| 102 | +public: |
| 103 | + int numTrees(int n) { |
| 104 | + vector<int>dp(n+1, 0); |
| 105 | + dp[0]=1; |
| 106 | + dp[1]=1; |
| 107 | + for(int i=2; i<=n; i++){ |
| 108 | + for(int j=1; j<=i; j++){ |
| 109 | + dp[i]= dp[i]+ dp[i-j]*dp[j-1]; |
| 110 | + } |
| 111 | + } |
| 112 | + return dp[n]; |
| 113 | + } |
| 114 | +}; |
| 115 | +``` |
0 commit comments