Skip to content

Commit dd1d0fe

Browse files
committed
96.不同的二叉搜索树,动态规划,递归
1 parent d39adb2 commit dd1d0fe

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

leetcode_Java/DoneTitle.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
92. 反转链表 II
5151
93. 复原 IP 地址
5252
94. 二叉树的中序遍历
53+
96. 不同的二叉搜索树
5354
101. 对称二叉树
5455
102. 二叉树的层序遍历
5556
103. 二叉树的锯齿形层序遍历

leetcode_Java/DoneType.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
一、树
1515
94. 二叉树的中序遍历
16+
96. 不同的二叉搜索树
1617
101. 对称二叉树
1718
102. 二叉树的层序遍历
1819
103. 二叉树的锯齿形层序遍历
@@ -67,6 +68,7 @@
6768
64. 最小路径和
6869
70. 爬楼梯
6970
72. 编辑距离
71+
96. 不同的二叉搜索树
7072
115. 不同的子序列
7173
121. 买卖股票的最佳时机
7274
122. 买卖股票的最佳时机 II
@@ -177,6 +179,7 @@
177179
704. 二分查找
178180
867. 转置矩阵(置换)
179181

182+
180183
八、字符串
181184
3. 无重复字符的最长子串(滑动窗口)
182185
8. 字符串转换整数 (atoi)

leetcode_Java/Solution0096.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 96. 不同的二叉搜索树
2+
3+
4+
/*
5+
动态规划:
6+
1、定义dp数组:dp[i] 表示i个节点组成二叉搜索树的种数
7+
2、初始化:dp[0]=1,dp[1]=1,表示0或1个节点时的种数为1。0个节点的种数为1,用于计算左右子树其中一个为空时不影响乘积结果
8+
3、状态转移方程:
9+
1)根据二叉搜索树的特性,左边的节点值 < 中间的节点值 < 右边的节点值
10+
2)总共有i个节点时,节点值j作为根节点时,那么左边有j-1个节点,右边有i-j个节点,种数为左边的种数乘以右边的种数,即 dp[i] = dp[j - 1] * dp[i - j];
11+
2)由于每个节点都可以作为根节点,所以要累加计算每个节点作为根节点时的种数,即 dp[i] += dp[j - 1] * dp[i - j];
12+
4、遍历dp数组填表:
13+
第一个for循环遍历dp数组,从2个节点开始遍历到n个节点
14+
第二个for循环遍历每个节点作为根节点时的种数,将种数累加
15+
5、返回结果:最后一个状态就是结果
16+
*/
17+
class Solution {
18+
public int numTrees(int n) {
19+
int[] dp = new int[n + 1];
20+
dp[0] = 1;
21+
dp[1] = 1;
22+
for (int i = 2; i <= n; i++) {
23+
for (int j = 1; j <= i; j++) {
24+
dp[i] += dp[j - 1] * dp[i - j];
25+
}
26+
}
27+
return dp[n];
28+
}
29+
}
30+
31+
32+
/*
33+
递归 + 记忆存储
34+
定义递归函数:
35+
1、方法功能:入参是节点数,返回对应的种数
36+
2、终止条件:节点数为0或1时,返回种数为1
37+
3、返回结果:节点数大于1时,要拿左右的种数相乘,返回乘积种数
38+
4、递归逻辑:
39+
1)总共有n个节点时,节点值i作为根节点时,那么左边有i-1个节点,右边有n-i个节点
40+
2)由于左右两边的种数同样要计算,因此调用同样的方法获取结果,将左右的种数相乘
41+
3)由于每个节点都可以作为根节点,所以要累加计算每个节点作为根节点时的种数
42+
4)计算n个节点的种数,存在重复计算,因此使用记忆存储,存在则直接返回结果
43+
*/
44+
class Solution {
45+
Map<Integer, Integer> memo = new HashMap<>();
46+
47+
public int numTrees(int n) {
48+
if (n == 0 || n == 1) {
49+
return 1;
50+
}
51+
if (memo.containsKey(n)) {
52+
return memo.get(n);
53+
}
54+
int count = 0;
55+
for (int i = 1; i <= n; i++) {
56+
int leftNum = numTrees(i - 1);
57+
int rightNum = numTrees(n - i);
58+
count += leftNum * rightNum;
59+
}
60+
memo.put(n, count);
61+
return count;
62+
}
63+
}

leetcode_Java/Solution0337.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public int rob(TreeNode root) {
4646
}
4747
int money = root.val;
4848
if (root.left != null) {
49-
money += (rob(root.left.left) + rob(root.left.right));
49+
money += rob(root.left.left) + rob(root.left.right);
5050
}
5151
if (root.right != null) {
52-
money += (rob(root.right.left) + rob(root.right.right));
52+
money += rob(root.right.left) + rob(root.right.right);
5353
}
5454
return Math.max(money, rob(root.left) + rob(root.right));
5555
}
@@ -71,10 +71,10 @@ public int rob(TreeNode root) {
7171
}
7272
int money = root.val;
7373
if (root.left != null) {
74-
money += (rob(root.left.left) + rob(root.left.right));
74+
money += rob(root.left.left) + rob(root.left.right);
7575
}
7676
if (root.right != null) {
77-
money += (rob(root.right.left) + rob(root.right.right));
77+
money += rob(root.right.left) + rob(root.right.right);
7878
}
7979
int maxMoney = Math.max(money, rob(root.left) + rob(root.right));
8080
memo.put(root, maxMoney);

0 commit comments

Comments
 (0)