## DEV Community is a community of 786,090 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Nic

Posted on • Originally published at coderscat.com on

# LeetCode: Unique Binary Search Trees II

How to generate all the unique binary search trees with given values [1, N].

## Depth-first search

We iterate each element in the array, use current element(suppose as k) as root node, construct left subtrees with [1, k-1], and construct right subtrees with [k+1, N].

This is a depth first search algorithm:

``````class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
vector<TreeNode*> empty;
return n == 0 ? empty : dfs(1, n);
}

vector<TreeNode*> dfs(int begin, int end){
vector<TreeNode*> res;
if(begin > end){
res.push_back(NULL);
return res;
}

if(begin == end){
TreeNode* pre = new TreeNode(begin);
res.push_back(pre);
return res;
}

for(int i=begin; i<=end; i++){
vector<TreeNode*> left = dfs(begin, i-1);
vector<TreeNode*> right = dfs(i+1, end);
for(int m = 0; m < left.size(); m++){
for(int n = 0; n < right.size(); n++){
TreeNode* pre = new TreeNode(i);
pre->left = left[m];
pre->right = right[n];
res.push_back(pre);
}
}
}
return res;
}
};
``````

## Optimization with memo

When we iterate the ranges, there are some duplicated computations could be cached.

For example, dfs(1, 2) is computed when 3 is root, it will be computed again when 4 is root.

So we could add a `map` to cache the result:

``````public:
map<pair<int,int>, vector<TreeNode*> > hmap;
vector<TreeNode*> generateTrees(int n) {
vector<TreeNode*> empty;
return n == 0 ? empty : dfs(1, n);
}

vector<TreeNode*> dfs(int begin, int end){
vector<TreeNode*> res;
pair<int, int> key(begin, end);
if(hmap.count(key))
return hmap[key];

if(begin > end){
res.push_back(NULL);
return res;
}

if(begin == end){
TreeNode* pre = new TreeNode(begin);
res.push_back(pre);
hmap[pair<int, int>(begin, end)] = res;
return res;
}

for(int i=begin; i<=end; i++){
vector<TreeNode*> left = dfs(begin, i-1);
vector<TreeNode*> right = dfs(i+1, end);
for(int m = 0; m < left.size(); m++){
for(int n = 0; n < right.size(); n++){
TreeNode* pre = new TreeNode(i);
pre->left = left[m];
pre->right = right[n];
res.push_back(pre);
}
}
}
hmap[key] = res;
return res;
}
};
``````

The post LeetCode: Unique Binary Search Trees II appeared first on Coder's Cat.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}