Description
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.
Example
1 | Input: 3 |
Solution
Method 1: Basic recursion method1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<TreeNode> generateTrees(int n) {
if (n == 0) return new ArrayList<TreeNode>();
return helper(1, n);
}
public List<TreeNode> helper(int left, int right){
List<TreeNode> res = new ArrayList<>();
if (left > right) {
res.add(null);
return res;
}
for (int i = left; i <= right; i++){
List<TreeNode> leftTree = helper(left, i - 1);
List<TreeNode> rightTree = helper(i + 1, right);
for (TreeNode leftNode: leftTree){
for (TreeNode rightNode: rightTree){
TreeNode newNode = new TreeNode(i);
newNode.left = leftNode;
newNode.right = rightNode;
res.add(newNode);
}
}
}
return res;
}
}
Method2: Recursion with momory, a kind of DP. Used memory[i][j][] to store BSTs constructed by i..j. In this method, used list which need to be initilized to store. The time for initialization if O^2, it’s not good. Maybe can change to Array to store.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<TreeNode> generateTrees(int n) {
if (n == 0) return new ArrayList<TreeNode>();
List<List<List<TreeNode>>> memory = new ArrayList<>();
for (int i = 0; i < n; i++){
memory.add(new ArrayList<>());
for (int j = 0; j < n; j++) memory.get(i).add(new ArrayList<TreeNode>());
}
return helper(1, n, memory);
}
public List<TreeNode> helper(int left, int right, List<List<List<TreeNode>>> memory){
List<TreeNode> res = new ArrayList<>();
if (left > right) {
res.add(null);
return res;
}
if (memory.get(left - 1).get(right - 1).size() != 0) return memory.get(left - 1).get(right - 1);
for (int i = left; i <= right; i++){
List<TreeNode> leftTree = helper(left, i - 1, memory);
List<TreeNode> rightTree = helper(i + 1, right, memory);
for (TreeNode leftNode: leftTree){
for (TreeNode rightNode: rightTree){
TreeNode newNode = new TreeNode(i);
newNode.left = leftNode;
newNode.right = rightNode;
res.add(newNode);
}
}
}
List<TreeNode> temp = memory.get(left - 1).get(right - 1);
temp = res;
return res;
}
}