96Unique Binary Search Trees – Medium¶
Problem:¶
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?
For example, Given n = 3, there are a total of 5 unique BST’s.
Thoughts:¶
Define nums[i] is the number of unique BST’s for given n = i.
Base case is nums[0] = nums[1] = 1
nums[i] = SUM of (nums[j] + nums[ i – j + 1]) while 0 <= j < i.
j is the number of elements in left subtree.