当前位置:网站首页>不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
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)記憶搜索用於避免重複計算,降低時間複雜度,尤其是指數級時間複雜度。
參考文獻
边栏推荐
- [线性代数] 1.2 全排列和对换
- What is the gold content of the equipment supervisor certificate? Is it worth it?
- Probe into metacosmic storage, the next explosive point in the data storage market?
- [線性代數] 1.1 二階與三階行列式
- allegro设置网络飞线以及网络颜色的方法
- Web GIS 航拍实现的智慧园区数字孪生应用
- [test theory] quality analysis ability
- Merge sort
- Etcd tutorial - Chapter 6 etcd core API V3
- 2022-2028 global bubble CPAP system industry survey and trend analysis report
猜你喜欢

PWN beginner level0

蓝桥杯2022初赛——扫雷

allegro对走好的线取消走线的方法

Problem - ADB shellerror: insufficient permissions for device: verify udev rules

FortiGate firewall configuration log uploading regularly

The method of displaying or closing the network flying line in Allegro design
[email protected]"/>无法定位程序输入点 [email protected]

2022-2028 global UAV detection radar industry research and trend analysis report

Etcd教程 — 第六章 Etcd之核心API V3
![Jerry's monitoring alarm clock [chapter]](/img/b7/a5ca7a79af51bd79e4f5c1322b72ff.jpg)
Jerry's monitoring alarm clock [chapter]
随机推荐
SQL continuous login problem
Jerry's monitoring alarm clock [chapter]
Connect error: no route to host (errno:113)
FortiGate firewall configuration log uploading regularly
[leetcode daily question] number of schemes to reconstruct a tree
set time format
allegro对走好的线取消走线的方法
无法定位程序输入点 [email protected]
1110: nearest common ancestor (function topic)
Tkinter Huarong Road 4x4 Tutorial 4
18. `bs object Node name next_ Sibling` get sibling nodes
SVN常用的十个命令
Applet view creation
Allegro's method of canceling the routing of a good line
How to add the live video function to the website built by your own live video software (build a live video website)
2022-2028 global pneumatic test probe industry survey and trend analysis report
【云原生】这么火,你不来了解下?
图扑软件智慧能源一体化管控平台
Faster memcpy alternatives- faster alternative to memcpy?
99 multiplication table