当前位置:网站首页>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;
}
边栏推荐
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- Learning multi-level structural information for small organ segmentation
- Take you to quickly learn how to use qsort and simulate qsort
- The end of the Internet is rural revitalization
- Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
- Vant --- detailed explanation and use of list component in vant
- Bicolor case
- ABAP:OOALV实现增删改查功能
- 70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
- Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
猜你喜欢

JS arguments parameter usage and explanation

HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)

gslb(global server load balance)技术的一点理解

Inputstream/outputstream (input and output of file)

Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout

C réaliser des jeux de serpents gourmands

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

每周小结(*63):关于正能量

如何避免 JVM 内存泄漏?

如何获取el-tree中所有节点的父节点
随机推荐
win10清除快速访问-不留下痕迹
每周小结(*63):关于正能量
Nexus 6p从8.0降级6.0+root
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
px em rem的区别
27-31. Dependency transitivity, principle
How to get the parent node of all nodes in El tree
Abap:ooalv realizes the function of adding, deleting, modifying and checking
2022.7.2-----leetcode. eight hundred and seventy-one
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
分布式CAP理论
Tutle clock improved version
对List进行排序工具类,可以对字符串排序
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
剑指 Offer II 038. 每日温度
buuctf-pwn write-ups (8)
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
JS arguments parameter usage and explanation
Reading notes of Clickhouse principle analysis and Application Practice (4)
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文