当前位置:网站首页>打家劫舍III[后序遍历与回溯+动态规划]
打家劫舍III[后序遍历与回溯+动态规划]
2022-07-06 11:21:00 【REN_林森】
前言
对于树问题,存在 前序与DFS/中序与平衡二叉树/后序与回溯/层序与BFS。对于打家劫舍,是一道经典的动态规划问题,当线性的数组转变成了分支的树结构,状态又该如何从两条分支去转移?
可以配合回溯,得到左右孩子两条分支的节点存钱情况,在根据实际需求,进行存钱选取。
一、打家劫舍III

状态转移没细细思考时,碰到的树结构。
二、回溯+动态规划
package everyday.tree;
public class Rob {
// 利用回溯来配合动态规划。
public int rob(TreeNode root) {
int[] r = order(root);
return Math.max(r[0], r[1]);
}
private int[] order(TreeNode root) {
if (null == root) return new int[]{
0, 0};
int[] l = order(root.left);
int[] r = order(root.right);
// 选根,就只能选择比较深的节点所存到的钱。
int first = root.val + l[0] + r[0];
// 不选根,这就值得考虑,在根的左孩子和更下一层就可以随便选一个,毕竟不会挨着根,右边同理。
int second = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
return new int[]{
second, first};
/* 二叉树问题: 前序遍历与DFS;中序遍历与平衡二叉树;后序遍历与回溯;层序遍历与BFS; */
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
总结
1)树问题的基础,莫过于,前序与DFS/中序与平衡二叉/后序与回溯/层序与BFS。
2)动态规划的状态转移,是需要值得细细思考的地方。
参考文献
[1] LeetCode 打家劫舍
边栏推荐
- The second day of rhcsa study
- Human bone point detection: top-down (part of the theory)
- test about BinaryTree
- Analysis of frequent chain breaks in applications using Druid connection pools
- 应用使用Druid连接池经常性断链问题分析
- 一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
- About NPM install error 1
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- Don't miss this underestimated movie because of controversy!
- QPushButton绑定快捷键的注意事项
猜你喜欢

五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
![[depth first search] Ji suanke: Square](/img/fc/e42ae0d036be258bed5623d55fc2db.jpg)
[depth first search] Ji suanke: Square

Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
![Optical blood pressure estimation based on PPG and FFT neural network [translation]](/img/88/2345dac73248a5f0f9fa3142ca0397.png)
Optical blood pressure estimation based on PPG and FFT neural network [translation]

Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform

星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
![A wearable arm device for night and sleeveless blood pressure measurement [translation]](/img/fd/947a38742ab1c4009ec6aa7405a573.png)
A wearable arm device for night and sleeveless blood pressure measurement [translation]

提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期

Jushan database was among the first batch of financial information innovation solutions!

How word displays modification traces
随机推荐
Take a look at how cabloyjs workflow engine implements activiti boundary events
Online notes
美庐生物IPO被终止:年营收3.85亿 陈林为实控人
多线程基础:线程基本概念与线程的创建
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
基于蝴蝶种类识别
helm部署etcd集群
Camel case with Hungarian notation
Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
根据PPG估算血压利用频谱谱-时间深度神经网络【翻】
How to type multiple spaces when editing CSDN articles
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Simple understanding of MySQL database