当前位置:网站首页>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;
}
边栏推荐
- How to expand all collapse panels
- Manually page the list (parameter list, current page, page size)
- Kubernets first meeting
- 每周小结(*63):关于正能量
- JS how to convert seconds into hours, minutes and seconds display
- Leakage detection relay jy82-2p
- The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
- How to determine whether an array contains an element
- 配置交叉编译工具链和环境变量
- Bicolor case
猜你喜欢

How to solve the component conflicts caused by scrollbars in GridView

740. Delete and get points
![70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment](/img/3f/36a67544deceb3d3789b500efde89c.jpg)
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment

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

Grounding relay dd-1/60

Kubernets first meeting

复合非线性反馈控制(二)

win10清除快速访问-不留下痕迹

C實現貪吃蛇小遊戲

4G wireless all network solar hydrological equipment power monitoring system bms110
随机推荐
Matlab remainder
Nexus 6p downgraded from 8.0 to 6.0+root
Qt发布多语言国际化翻译
如何实现视频平台会员多账号登录
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
注释与注解
Reading notes of Clickhouse principle analysis and Application Practice (4)
4G wireless all network solar hydrological equipment power monitoring system bms110
如何展开Collapse 的所有折叠面板
Appium基础 — APPium安装(二)
2022.7.2-----leetcode.871
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
ES6 模块化
px em rem的区别
AWT common components, FileDialog file selection box
《ClickHouse原理解析与应用实践》读书笔记(4)
How to expand all collapse panels
云原生——上云必读之SSH篇(常用于远程登录云服务器)