Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita3b9763

Browse files
author
applewjg
committed
Unique Binary Search Trees I &&II
Change-Id: Id843060bbea224d18311f6e0f712cf110c8f826a
1 parente068f1a commita3b9763

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, higuige@gmail.com
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+
publicclassSolution {
20+
publicintnumTrees_1(intn) {
21+
int[]dp =newint[n+1];
22+
dp[0] =1;
23+
for (inti =1;i <=n; ++i)
24+
for (intj =0;j <i;j++)
25+
dp[i] +=dp[j] *dp[i-j-1];
26+
returndp[n];
27+
}
28+
publicintnumTrees(intn) {
29+
if (n <0)return0;
30+
int[]dp =newint[n+1];
31+
dp[0] =1;dp[1] =1;
32+
for(inti =2;i <=n; ++i){
33+
dp[i] =dp[i-1] * (4 *i -2)/(i +1);
34+
}
35+
returndp[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, wangjingui@outlook.com
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+
publicclassSolution {
33+
publicList<TreeNode>generateTrees(intn) {
34+
returngenerateTreesRe(1,n);
35+
}
36+
publicList<TreeNode>generateTreesRe(intl,intr) {
37+
ArrayList<TreeNode>res =newArrayList<TreeNode>();
38+
if (l >r) {
39+
res.add(null);
40+
returnres;
41+
}
42+
for (intk =l;k <=r; ++k) {
43+
List<TreeNode>leftTrees =generateTreesRe(l,k-1);
44+
List<TreeNode>rightTrees =generateTreesRe(k+1,r);
45+
for (inti =0;i <leftTrees.size();i++) {
46+
for (intj =0;j <rightTrees.size();j++) {
47+
TreeNoderoot =newTreeNode(k);
48+
root.left =leftTrees.get(i);
49+
root.right =rightTrees.get(j);
50+
res.add(root);
51+
}
52+
}
53+
}
54+
returnres;
55+
}
56+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp