当前位置:网站首页>二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
2022-06-24 13:00:00 【REN_林森】
前言
对于二叉树而言,利用好本身的递归体质,是解决问题的关键,处理好递归中任意一棵子树,就处理好了整个树。通过二叉树中最大路径和练习回溯+处理子树。
一、二叉树中最大路径和


二、回溯+处理子树
package everyday.hard;
// 二叉树中的最大路径和。
public class MaxPathSum {
/* target:找到任意路径和最大的值。 从递归结构来看的话,左右子树想要有路径,那必然只能在左右子树选一个孩子,通过父节点连通。 而要选最大和的路径值,可用一个变量记录max即可。 */
public int maxPathSum(TreeNode root) {
// 在遍历过程中寻找最大值。
order(root);
// 返回寻找到的最大值。
return max;
}
private int max = 1 << 31;
private int order(TreeNode root) {
// 访问到null节点,递归结束,返回贡献值0.
if (null == root) return 0;
// 回溯得到 左右值。
int left = order(root.left);
int right = order(root.right);
// 四种可选路径,左根 | 右根 | 根 | 左根右
// 返回的路径和只能在前三种中选最大值,而max化,需要在四种路径中取最大值。
// 先找前三者谁大,谁大,返回谁;再和左根右比较,跟max比较,得到max.
int rs = Math.max(left + root.val, Math.max(right + root.val, root.val));
// 更新max
max = Math.max(max, Math.max(rs, left + right + root.val));
// 返回最大子路径和
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;
}
}
}
总结
1)处理好每一颗子树。
2)二叉树处理问题,要么从上到下,要么从下往上看问题。
参考文献
边栏推荐
- 2022煤矿瓦斯抽采操作证考试题及模拟考试
- Rongyun communication has "hacked" into the heart of the bank
- Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
- js去除字符串空格
- 在线文本实体抽取能力,助力应用解析海量文本数据
- Jerry's seamless looping [chapter]
- OpenHarmony 1
- Kotlin asynchronous flow
- Operation of simulated examination platform for examination questions of coal production and operation units (safety production management personnel) in 2022
- 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!
猜你喜欢

Antd checkbox, limit the selected quantity

HarmonyOS.2

HarmonyOS-3

万用表测量电阻图解及使用注意事项

Getting to know cloud native security for the first time: the best guarantee in the cloud Era

Gatling 性能测试

一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?

杰理之串口接收 IO 需要设置数字功能【篇】

The research on the report "market insight into China's database security capabilities, 2022" was officially launched

厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构
随机推荐
Kotlin asynchronous flow
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
万用表的使用方法
Kotlin shared mutable state and concurrency
Dragon lizard developer said: first time you got an electric shock, so you are such a dragon lizard community| Issue 8
SAP Marketing Cloud 功能概述(三)
杰理之红外滤波【篇】
文本对比学习综述
The real project managers are all closed-loop masters!
greendao使用问题
2022 coal mine gas drainage operation certificate examination questions and simulation examination
2022 construction elevator driver (construction special type of work) examination questions and online simulation examination
【5G NR】5G NR系统架构
Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
数学之英文写作——基本中英文词汇(几何与三角的常用词汇)
SAP Marketing Cloud 功能概述(四)
【深度学习】NCHW、NHWC和CHWN格式数据的存储形式
项目经理搭建团队,需要看6个特征
返回新列表
2022煤矿瓦斯抽采操作证考试题及模拟考试