当前位置:网站首页>Looting iii[post sequence traversal and backtracking + dynamic planning]
Looting iii[post sequence traversal and backtracking + dynamic planning]
2022-07-06 19:13:00 【REN_ Linsen】
Backtracking joint dynamic programming
Preface
For tree problems , There is Preface and DFS/ Medium order and balanced binary tree / Postscript and backtracking / Sequence and BFS. For home raiding , It is a classic dynamic programming problem , When a linear array is transformed into a branched tree structure , How should the state be transferred from two branches ?
Can cooperate with backtracking , Get the node savings of the two branches of the left and right children , According to actual needs , Choose to save money .
One 、 raid homes and plunder houses III

When the state transition doesn't think carefully , Encountered tree structure .
Two 、 to flash back + Dynamic programming
package everyday.tree;
public class Rob {
// Use backtracking to cooperate with dynamic programming .
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);
// Root selection , You can only choose the money saved by the deeper node .
int first = root.val + l[0] + r[0];
// Don't choose the root , This is worth considering , In the left child of the root and the next layer, you can choose any one , After all, it won't be next to the root , The same goes for the right side .
int second = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
return new int[]{
second, first};
/* Binary tree problem : Preorder traversal and DFS; Middle order traversal and balanced binary tree ; Post order traversal and backtracking ; Sequence traversal and 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;
}
}
}
summary
1) The basis of the tree problem , is , Preface and DFS/ Middle order and balanced binary / Postscript and backtracking / Sequence and BFS.
2) State transition of Dynamic Planning , It is a place worthy of careful consideration .
reference
边栏推荐
- How to improve website weight
- Benefit a lot, Android interview questions
- About static type, dynamic type, ID, instancetype
- RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
- R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
- Implementation of AVL tree
- 业务与应用同步发展:应用现代化的策略建议
- R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
- In 50W, what have I done right?
- QPushButton绑定快捷键的注意事项
猜你喜欢

渲大师携手向日葵,远控赋能云渲染及GPU算力服务

AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】

应用使用Druid连接池经常性断链问题分析

AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?

How are you in the first half of the year occupied by the epidemic| Mid 2022 summary

C language daily practice - day 22: Zero foundation learning dynamic planning

ACTF 2022圆满落幕,0ops战队二连冠!!

三面蚂蚁金服成功拿到offer,Android开发社招面试经验

多线程基础:线程基本概念与线程的创建

Word如何显示修改痕迹
随机推荐
驼峰式与下划线命名规则(Camel case With hungarian notation)
[depth first search] Ji suanke: find numbers
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
手写一个的在线聊天系统(原理篇1)
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
黑马--Redis篇
First day of rhcsa study
Pytorch common loss function
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
QLabel 跑马灯文字显示
ROS custom message publishing subscription example
PMP每日一练 | 考试不迷路-7.6
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
受益匪浅,安卓面试问题
About NPM install error 1
Implementation of AVL tree
Handwritten online chat system (principle part 1)
如何提高网站权重
Use map function and split function to type multiple elements in one line
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