当前位置:网站首页>不同的二叉搜索树[自下而上回溯生成树+记忆搜索--空间换时间]
不同的二叉搜索树[自下而上回溯生成树+记忆搜索--空间换时间]
2022-06-29 03:16: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)记忆搜索用于避免重复计算,降低时间复杂度,尤其是指数级时间复杂度。
参考文献
边栏推荐
- 微信小程序安全登录,必须先校验微信返回openid,确保信息唯一性。
- Redu.us took the initiative to transform, and the operation leader of China's charging pile emerged
- 2022年启牛学堂证券开户安全的嘛?
- Ten commands commonly used in SVN
- The method of displaying or closing the network flying line in Allegro design
- Is the account opening of GF Securities really safe and reliable
- Applet view creation
- The method of exporting packages of all components from existing PCBs in Altium Designer
- 2022-2028 global CAE engineering service industry research and trend analysis report
- Basic MySQL database operations
猜你喜欢
![Jerry's monitoring alarm clock [chapter]](/img/b7/a5ca7a79af51bd79e4f5c1322b72ff.jpg)
Jerry's monitoring alarm clock [chapter]

The method of displaying or closing the network flying line in Allegro design

Redu.us took the initiative to transform, and the operation leader of China's charging pile emerged
![[yunyuanyuan] it's so hot. Why don't you come and understand it?](/img/a8/99037ec5b796e39b9e76eac95deb86.png)
[yunyuanyuan] it's so hot. Why don't you come and understand it?

Tupu software intelligent energy integrated management and control platform

2022-2028 global long wave infrared camera and camera core industry research and trend analysis report

层次分析法(AHP)
![Jerry's watch begins to move [chapter]](/img/85/232da1c1f8dddb3af1e564e4a0fc53.jpg)
Jerry's watch begins to move [chapter]

18. `bs object Node name next_ Sibling` get sibling nodes
![Movement state change of monitoring device of Jerry's watch [chapter]](/img/ff/cbc9e50a7d64e943f9f9924eadb184.jpg)
Movement state change of monitoring device of Jerry's watch [chapter]
随机推荐
FarrowTech的无线传感器采用橙群微电子的NanoBeacon蓝牙信标技术
Is the account opening of GF Securities really safe and reliable
【云原生】这么火,你不来了解下?
Problème - Ajouter shellerror: permissions d'instrumentation pour le périphérique: vérifier les règles udev.
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
2022-2028 global sound insulation coating industry research and trend analysis report
想当设备管理师?满足这三个报考条件就可以
[leetcode daily question] number of schemes to reconstruct a tree
Certification training | streamnational certification training phase 2
There's a mystery behind the little login
深入解析 Apache BookKeeper 系列:第三篇——读取原理
How to optimize databases and tables
Logarithmic calculation in reverse order, logarithmic calculation in sequence -- merge sort
使用gdb添加断点的几种方式
Want to be an equipment manager? If you meet these three conditions, you can
Ten commands commonly used in SVN
[线性代数] 1.1 二阶与三阶行列式
STM32L4系列单片机ADC通过内部参考电压精确计算输入电压
SQL training 01
【雲原生】這麼火,你不來了解下?