当前位置:网站首页>Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
2022-06-24 14:06:00 【REN_ Linsen】
Recursive property
Preface
For binary trees , Make good use of its recursive constitution , Is the key to solving the problem , Handle any subtree in recursion , And you're done with the whole tree . Through the maximum path in the binary tree and practice backtracking + Dealing with subtrees .
One 、 Maximum path sum in binary tree


Two 、 to flash back + Dealing with subtrees
package everyday.hard;
// The maximum path in a binary tree and .
public class MaxPathSum {
/* target: Find any path and maximum value . From the perspective of recursive structure , The left and right subtrees want to have paths , It must be that only one child can be selected from the left and right subtrees , Connect through parent node . Choose the path value of the maximum sum , A variable can be used to record max that will do . */
public int maxPathSum(TreeNode root) {
// Find the maximum value in the traversal process .
order(root);
// Returns the maximum value found .
return max;
}
private int max = 1 << 31;
private int order(TreeNode root) {
// Access to the null node , Recursion ends , Return the contribution value 0.
if (null == root) return 0;
// Backtracking Left and right values .
int left = order(root.left);
int right = order(root.right);
// Four alternative paths , Left root | Right root | root | Left root right
// The returned path and can only be the maximum of the first three , and max turn , You need to take the maximum of the four paths .
// Find out who is the biggest of the first three , Who is big , Return to who ; Then compare it with the left root and the right , Follow max Compare , obtain max.
int rs = Math.max(left + root.val, Math.max(right + root.val, root.val));
// to update max
max = Math.max(max, Math.max(rs, left + right + root.val));
// Returns the maximum subpath and
return rs;
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
summary
1) Deal with each subtree .
2) Binary tree processing problem , Or from top to bottom , Or look at the problem from the bottom up .
reference
边栏推荐
- In the era of knowledge economy, it will teach you to do well in knowledge management
- 一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
- 2022 construction elevator driver (construction special type of work) examination questions and online simulation examination
- 谷歌WayMo提出R4D: 采用参考目标做远程距离估计
- 【R语言数据科学】(十四):随机变量和基本统计量
- 2022年烟花爆竹生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作
- Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
- Jerry's infrared filtering [chapter]
- Redis面试题
- 【深度学习】NCHW、NHWC和CHWN格式数据的存储形式
猜你喜欢

如何在物联网低代码平台中进行任务管理?

**Unity中莫名其妙得小问题-灯光和天空盒

初识云原生安全:云时代的最佳保障

杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】

HarmonyOS-3

The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance

Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices

Tupu software is the digital twin of offshore wind power, striving to be the first

数商云:加强供应商管理,助推航空运输企业与供应商高效协同

MIT-6.824-lab4A-2022(万字讲解-代码构建)
随机推荐
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
Prometheus pushgateway
【5G NR】5G NR系统架构
Tupu software is the digital twin of offshore wind power, striving to be the first
**Unity中莫名其妙得小问题-灯光和天空盒
The real project managers are all closed-loop masters!
win10系统问题
【sdx62】WCN685X IPA注册失败问题分析及解决方案
Docker安装redis
Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!
[5g NR] 5g NR system architecture
杰理之增加一个输入捕捉通道【篇】
AQS初探
kotlin 协程通道
90% of the project managers have skipped the pit. Are you still in the pit?
HarmonyOS-3
2022年质量员-设备方向-岗位技能(质量员)复训题库及在线模拟考试
2022 construction elevator driver (construction special type of work) examination questions and online simulation examination
Return to new list
Eight major trends in the industrial Internet of things (iiot)