当前位置:网站首页>不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
2022-06-29 03:18:00 【REN_林森】
前言
第一,對於二叉樹的生成,一般自上而下遞歸確定好子樹區間,自下而上回溯拼接生成樹。
第二,對於很多重複的計算,可以用額外的空間將其結果記住,以後再碰到就直接取即可,大大降低時間複雜度,一般就是指數級別的複雜度,特別需要這樣做。
一、不同的二叉搜索樹
二、生成樹+記憶搜索
1、遞歸回溯生成二叉樹
// 不同的二叉搜索樹。
public class NumTrees {
/* target:給定節點個數,返回不同二叉搜索樹的個數。 以每個節點為根節點,左右子樹的個數相乘就是不同二叉樹的個數。 自下而上,回溯生成樹。 */
public int numTrees(int n) {
return numTrees(1, n);
}
// 自下而上,回溯生成。
private int numTrees(int begin, int end) {
// 無法再進行劃分,返回左孩子的個數,1個null節點。
if (begin > end) return 1;
// 得到左右孩子的組合數。
int rs = 0;
for (int i = begin; i <= end; i++) {
// i作為根節點,劃分兩個區間,分別作為左右子樹。
rs += numTrees(begin, i - 1) * numTrees(i + 1, end);
}
return rs;
}
}
2、記憶搜索–空間換時間
// 上面的方案超時,如果僅僅是得到不同二叉樹個數,可以不用模擬每一次 區間生成子樹,第一次生成後就將其記錄,後面再碰到這個區間,直接取值即可。
class NumTrees2 {
/* target:給定節點個數,返回不同二叉搜索樹的個數。 以每個節點為根節點,左右子樹的個數相乘就是不同二叉樹的個數。 自下而上,回溯生成樹。 用空間換時間,定義f[i][j]:[i,j]生成的子樹有多少種可能,第一次就計算賦值,後面再碰到就不用算了,直接取出用即可。 */
public int numTrees(int n) {
// 定義[i,j]能生成多少子樹。
int[][] f = new int[n + 1][n + 1];
// 自下而上,回溯生成。
return numTrees(1, n, f);
}
// 自下而上,回溯生成。
private int numTrees(int begin, int end, int[][] f) {
// 無法再進行劃分,返回左孩子的個數,1個null節點。
if (begin > end) return 1;
// 記憶搜索
if (f[begin][end] != 0) return f[begin][end];
// 得到左右孩子的組合數。
// 第一次碰到這個區間,其得到的結果記住。
for (int i = begin; i <= end; i++) {
// i作為根節點,劃分兩個區間,分別作為左右子樹。
f[begin][end] += numTrees(begin, i - 1, f) * numTrees(i + 1, end, f);
}
// 返回該區間第一次計算得到的子樹個數。
return f[begin][end];
}
}
總結
1)如何生成一顆二叉樹,自上而下遞歸劃分左右區間,自下而上回溯拼接左右子樹。
2)記憶搜索用於避免重複計算,降低時間複雜度,尤其是指數級時間複雜度。
參考文獻
边栏推荐
- [yunyuanyuan] it's so hot. Why don't you come and understand it?
- Data discretization
- 2022-2028 global industrial lithium chloride industry research and trend analysis report
- 1110: 最近共同祖先(函数专题)
- Linear and nonlinear structures
- Yyds dry inventory difference between bazel and gradle tools
- In depth analysis of Apache bookkeeper series: Part 3 - reading principle
- vim配置与使用
- Analytic hierarchy process (AHP)
- 解决allegro中测量距离时,点击一个点后光标闪烁的问题
猜你喜欢
![[North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss](/img/84/c8604f539db4658049406080d7f09a.jpg)
[North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss

PWN attack and defense world level2

【线程通信】

allegro设置网络飞线以及网络颜色的方法

2022-2028 global sound insulation coating industry research and trend analysis report

Gartner's "voice of customers" has the highest score, and the user experience has become a major breakthrough for China's database

想当设备管理师?满足这三个报考条件就可以

There's a mystery behind the little login

matlab习题 —— 图像绘制练习

1110: 最近共同祖先(函数专题)
随机推荐
Data discretization
Tortoise does not display a green Icon
如何理解MySQL的索引?
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
如何在关闭socket连接的时候跳过TIME_WAIT的等待状态
Farrowtech's wireless sensor adopts the nanobeacon Bluetooth beacon technology of orange group Microelectronics
PWN attack and defense world level2
FPGA(八)RTL代码之四(基本电路设计1)
2022-2028 global CAE engineering service industry research and trend analysis report
Redu.us took the initiative to transform, and the operation leader of China's charging pile emerged
What is the gold content of the equipment supervisor certificate? Is it worth it?
PWN beginner level0
均贫富
Merge sort
设备监理师证书含金量怎样?值得考吗?
Analytic hierarchy process (AHP)
If you dare to write MQ message queue middleware on your resume, you must win these questions!
set time format
18. `bs object Node name next_ Sibling` get sibling nodes
快速排序,查询序列的第K大的数