


If N is 3,all unique BST will be:

The first line of each test case contains a single integer, ‘N’, denoting the number.
For each test case, print the level order traversal of all the unique BST formed.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= N <= 8.
Time limit: 1 sec
In this problem, we can recursively form all the unique BST for the range 1 to N.So if we make root as ‘K’ then the left subtree will be constructed by range 1 to K-1. and right subtree as K+1 to N.
So, we will define a recursive function REC(i,j) that will return the list of roots of unique BST formed by numbers in range i to j. So if we make the root node with the value ‘K’.Then the left child will be among the list corresponding to REC(i, K-1) and similarly right child among REC(K+1,j).
At last, we will return the list corresponding to REC(1,’N’).
As we prepare the recursive tree for Approach 1, we can observe that we are having multiple calls for the same subproblem.
In this approach, we will use the same recursive functions, we used in approach 1 but we will use memoization to reduce the complexity as the answer for each state will be calculated only once.
We will define a 2D array ‘DP’ to store the answers.
In this approach, we will use dynamic programming and find the answer to the problem. We will prepare 2D array of lists of root nodes ‘DP’. We will compute the list DP[i][j] using DP[i][K-1] and DP[K+1][j].