当前位置:网站首页>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
边栏推荐
- R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
- Test technology stack arrangement -- self cultivation of test development engineers
- Simple understanding of MySQL database
- [depth first search] Ji suanke: find numbers
- How to improve website weight
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- 青龙面板最近的库
- 【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
- [translation] a GPU approach to particle physics
- Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
猜你喜欢

Php+redis realizes the function of canceling orders over time
![[matlab] Simulink the input and output variables of the same module cannot have the same name](/img/99/adfe50075010916439cd053b8f04c7.png)
[matlab] Simulink the input and output variables of the same module cannot have the same name

LeetCode-1279. Traffic light intersection

ROS custom message publishing subscription example

Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

CCNP Part 11 BGP (III) (essence)

Describe the process of key exchange

黑马--Redis篇

RT-Thread 组件 FinSH 使用时遇到的问题

Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
随机推荐
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
A method of removing text blur based on pixel repair
First day of rhcsa study
Crawling data encounters single point login problem
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Interview assault 63: how to remove duplication in MySQL?
R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
LeetCode-1279. Traffic light intersection
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
Online notes
Describe the process of key exchange
Wx applet learning notes day01
PMP每日一练 | 考试不迷路-7.6
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30