当前位置:网站首页>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;
}
边栏推荐
- 740. Delete and get points
- 198. House raiding
- px em rem的区别
- How to implement lazy loading in El select (with search function)
- Webrtc quickly set up video call and video conference
- Win10 clear quick access - leave no trace
- 测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
- Component、Container容器常用API详解:Frame、Panel、ScrollPane
- Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
- Abap:ooalv realizes the function of adding, deleting, modifying and checking
猜你喜欢
Reading notes of Clickhouse principle analysis and Application Practice (4)
Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
How to avoid JVM memory leakage?
[microservice] Nacos cluster building and loading file configuration
体验碎周报第 102 期(2022.7.4)
树形dp
Design and implementation of redis 7.0 multi part AOF
Notes and notes
198. House raiding
C实现贪吃蛇小游戏
随机推荐
A little understanding of GSLB (global server load balance) technology
[March 3, 2019] MAC starts redis
QT 获取随机颜色值设置label背景色 代码
Invalid revision: 3.18.1-g262b901-dirty
How to avoid JVM memory leakage?
198. House raiding
MySQL installation and configuration
How to expand all collapse panels
JSON Web Token----JWT和传统session登录认证对比
Abap:ooalv realizes the function of adding, deleting, modifying and checking
Weekly summary (*63): about positive energy
Learning multi-level structural information for small organ segmentation
如何获取el-tree中所有节点的父节点
px em rem的区别
QT QTableWidget 表格列置顶需求的思路和代码
Understanding of cross domain and how to solve cross domain problems
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
Lightroom import picture gray / Black rectangular multi display
SQL injection SQL lab 11~22
分布式CAP理论