当前位置:网站首页>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;
}
边栏推荐
- buuctf-pwn write-ups (8)
- 【微服务】Nacos集群搭建以及加载文件配置
- Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
- SQL injection SQL lab 11~22
- Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
- 70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
- 双色球案例
- Tutle clock improved version
- Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
- Tsinghua University product: penalty gradient norm improves generalization of deep learning model
猜你喜欢

MySQL installation and configuration

JS扁平化数形结构的数组

509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs

How to realize multi account login of video platform members

C实现贪吃蛇小游戏

Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)

Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.

如何避免 JVM 内存泄漏?

Distributed cap theory

MySQL learning notes 3 - JDBC
随机推荐
2022.7.2-----leetcode. eight hundred and seventy-one
C realize Snake games
Kubernets first meeting
Design and implementation of redis 7.0 multi part AOF
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Fast power (template)
JSON web token -- comparison between JWT and traditional session login authentication
How to get the parent node of all nodes in El tree
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
Sleep quality today 78 points
The width of the picture in rich text used by wechat applet exceeds the problem
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
QT qtablewidget table column top requirements ideas and codes
Configure cross compilation tool chain and environment variables
My NVIDIA developer journey - optimizing graphics card performance
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Weekly summary (*63): about positive energy
Arcpy 利用updatelayer函数改变图层的符号系统