GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/L096_Unique_Binary_Search_Trees.java at master · likevson/LeetCode

Expand file tree

/

L096_Unique_Binary_Search_Trees.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
32 lines (25 loc) · 692 Bytes
/

L096_Unique_Binary_Search_Trees.java

Copy path

File metadata and controls

32 lines (25 loc) · 692 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package LeetCode;
public class L096_Unique_Binary_Search_Trees {
public int numTrees(int n) {
if (n == 1 || n == 2) {
return n;
}
// record[0]没有用,所以长度是n+1
// 使用数组,从下向上保存结果,能够节省时间,否则会超时
int[] record = new int[n + 1];
record[0] = 1;
record[1] = 1; // 1个元素时,情况为1
record[2] = 2; // 2个元素时,情况为2
for (int i = 3; i <= n; i++) {
int tmp = 0;
for (int k = 0; k < i; k++) {
// 以n为根结点的二叉树个数=左结点的二叉树个数*右结点的二叉树个数
// 题目所求要包括所有情况,分别以1~n为根结点
tmp += (record[k] * record[i - k - 1]);
}
// 记录1~i时,BST的个数
record[i] = tmp;
}
return record[n];
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.