Saturday, August 5th
Unique Binary Search Trees
Given an integer n, return all unique binary search trees with n nodes, with values ranging from 1 to n
Photo courtesy of Sverrir Mirdsson via Wikipedia. Distributed under the Creative Commons Attribution-Share Alike 3.0 Unported license. No changes made.Many binary search tree (BST) problems involve recursion. All subtrees of binary search trees are themselves binary search trees. For this problem we will approach it recursively. Also, since many subproblems may overlap, we include a hashmap to memoize intermediate results and implement top-down dynamic programming.
We create a recursive function that, given a start and end value, generates all possible BSTs with the values [start,end]. We also create a memo map that stores the result for a pair of start/end nodes
First, we initialize a vector of TreeNodes to store the function result
Then, like with all recursive functions, we begin with the base case. Namely, if the start and end indices don't line up, or if start is greater than end, then return NULL as we've reached the end
Similarly, if the memo already contains a result, return it and save some work
Now, the recursive function contains two parameters, start and end. We want to find all possible binary search trees with nodes from start to end. Initially the start and end values are 0 and n-1, but this applies to all subtrees.
We iterate from start to end, selecting each value as a root. This means that once a root is found at i, then the left subtree contains nodes [start,i-1] and the right subtree contains nodes [i+1, end]. Using this information we recursively call the BST function.
Now with all possible left and right subtrees, we iterate through both and generate all possible combinations of left and right
For each combination, we create a root node, set its left and right child to the root of the left and right subtrees. Then add the tree to the result vector
Finally, add the result to the memo, keying the vector of trees with the start and end node values
Thanks to the Editorial for a clear and concise explanation
Comments
Post a Comment