当前位置:网站首页>Tree DP
Tree DP
2022-07-04 06:19:00 【Enthusiastic citizen Mr Xue】
Tree form dp It is in the form of binary tree dp, This problem uses an array to save the results of the current node .
res[0] Indicates that the current node is not stolen res[1] Represents the current node
public int rob(TreeNode root) {
int[] res = recur(root);
return Math.max(res[0],res[1]);
}
public int[] recur(TreeNode root){
int []res = new int[2];
The current node is empty return
if(root == null) return res;
Recursive left and right nodes
int[] left = recur(root.left);
int[] right = recur(root.right);
Do not steal the current node , You can steal the nodes of left and right children
res[0] = Math.max(left[0],left[1]) + Math.max(right[0],right[1]);
Steal the current node , Then don't steal the left and right child nodes ,[0] Represents that the value of the current node is not stolen
res[1] = root.val + left[0] + right[0];
return res;
}
边栏推荐
- MySQL的information_schema数据库
- "In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
- [untitled]
- Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
- The width of the picture in rich text used by wechat applet exceeds the problem
- 2022.7.2-----leetcode.871
- 雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
- Vant --- detailed explanation and use of list component in vant
- lightroom 导入图片灰色/黑色矩形 多显示器
- Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
猜你喜欢
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
如何展开Collapse 的所有折叠面板
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
JSON Web Token----JWT和傳統session登錄認證對比
Component、Container容器常用API详解:Frame、Panel、ScrollPane
Arcpy 利用updatelayer函数改变图层的符号系统
每周小结(*63):关于正能量
Win10 clear quick access - leave no trace
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
随机推荐
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
每周小结(*63):关于正能量
Tutle clock improved version
JSON Web Token----JWT和傳統session登錄認證對比
The difference between PX EM rem
JS arguments parameter usage and explanation
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
Sleep quality today 78 points
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
How to help others effectively
Bicolor case
如何避免 JVM 内存泄漏?
MySQL information_ Schema database
《ClickHouse原理解析与应用实践》读书笔记(4)
分布式CAP理论
QT get random color value and set label background color code
JSON Web Token----JWT和传统session登录认证对比
JS execution mechanism
SQL join, left join, right join usage