Skip to content

Commit a3b9763

Browse files
author
applewjg
committed
Unique Binary Search Trees I &&II
Change-Id: Id843060bbea224d18311f6e0f712cf110c8f826a
1 parent e068f1a commit a3b9763

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

UniqueBinarySearchTrees.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Oct 07, 2014
4+
Problem: Unique Binary Search Trees
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/unique-binary-search-trees/
7+
Notes:
8+
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
9+
For example,
10+
Given n = 3, there are a total of 5 unique BST's.
11+
1 3 3 2 1
12+
\ / / / \ \
13+
3 2 1 1 3 2
14+
/ / \ \
15+
2 1 2 3
16+
17+
Solution: dp.
18+
*/
19+
public class Solution {
20+
public int numTrees_1(int n) {
21+
int[] dp = new int[n+1];
22+
dp[0] = 1;
23+
for (int i = 1; i <= n; ++i)
24+
for (int j = 0; j < i; j++)
25+
dp[i] += dp[j] * dp[i-j-1];
26+
return dp[n];
27+
}
28+
public int numTrees(int n) {
29+
if (n < 0) return 0;
30+
int[] dp = new int[n+1];
31+
dp[0] = 1; dp[1] = 1;
32+
for(int i = 2;i <= n; ++i){
33+
dp[i] = dp[i-1] * (4 * i - 2)/(i + 1);
34+
}
35+
return dp[n];
36+
}
37+
}

UniqueBinarySearchTreesII.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Unique Binary Search Trees II
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/unique-binary-search-trees-ii/
7+
Notes:
8+
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
9+
For example,
10+
Given n = 3, your program should return all 5 unique BST's shown below.
11+
1 3 3 2 1
12+
\ / / / \ \
13+
3 2 1 1 3 2
14+
/ / \ \
15+
2 1 2 3
16+
17+
Solution: 1. DFS directly. (from the Internet)
18+
2. DP + DFS. (my solution)
19+
a. Generate trees for 'n' from 1 to n. (DP)
20+
b. When generate trees for n = i, get the left and right subtrees
21+
by copying tree structures of dp[1...i-1]. (copy tree uses DFS)
22+
*/
23+
/**
24+
* Definition for binary tree
25+
* public class TreeNode {
26+
* int val;
27+
* TreeNode left;
28+
* TreeNode right;
29+
* TreeNode(int x) { val = x; left = null; right = null; }
30+
* }
31+
*/
32+
public class Solution {
33+
public List<TreeNode> generateTrees(int n) {
34+
return generateTreesRe(1, n);
35+
}
36+
public List<TreeNode> generateTreesRe(int l, int r) {
37+
ArrayList<TreeNode> res = new ArrayList<TreeNode>();
38+
if (l > r) {
39+
res.add(null);
40+
return res;
41+
}
42+
for (int k = l; k <= r; ++k) {
43+
List<TreeNode> leftTrees = generateTreesRe(l, k-1);
44+
List<TreeNode> rightTrees = generateTreesRe(k+1, r);
45+
for (int i = 0; i < leftTrees.size(); i++) {
46+
for (int j = 0; j < rightTrees.size(); j++) {
47+
TreeNode root = new TreeNode(k);
48+
root.left = leftTrees.get(i);
49+
root.right = rightTrees.get(j);
50+
res.add(root);
51+
}
52+
}
53+
}
54+
return res;
55+
}
56+
}

0 commit comments

Comments
 (0)