当前位置:网站首页>二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
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)二叉树处理问题,要么从上到下,要么从下往上看问题。
参考文献
边栏推荐
- 一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
- The hidden corner of codefarming: five things that developers hate most
- 真正的项目经理强者,都是闭环高手!
- SAP Marketing Cloud 功能概述(三)
- 90%的项目经理都跳过的坑,你现在还在坑里吗?
- Kotlin shared mutable state and concurrency
- Daily question 8-515 Find the maximum value in each tree row
- 从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
- 《中国数据库安全能力市场洞察,2022》报告研究正式启动
- Jerry's test mic energy automatic recording automatic playback reference [article]
猜你喜欢

HarmonyOS-3

OpenHarmony 1

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

智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理

Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!

AntD checkbox,限制选中数量

These default routes and static routes can not be configured and deployed. What kind of network workers are they!

HarmonyOS.2

Daily question 8-515 Find the maximum value in each tree row

Google waymo proposed r4d: remote distance estimation using reference target
随机推荐
真正的项目经理强者,都是闭环高手!
How to manage tasks in the low code platform of the Internet of things?
杰理之红外滤波【篇】
[5g NR] 5g NR system architecture
Seven challenges faced by data scientists and Solutions
Dragon lizard developer said: first time you got an electric shock, so you are such a dragon lizard community| Issue 8
2022年江西省安全员B证考试题库模拟考试平台操作
《中国数据库安全能力市场洞察,2022》报告研究正式启动
Ti Xing Shu'an joined the dragon lizard community to jointly create a network security ecosystem
21set classic case
OpenHarmony 1
2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
Usage of multimeter
English writing of Mathematics -- Basic Chinese and English vocabulary (common vocabulary of geometry and trigonometry)
Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
kotlin 异步流
kotlin 协程通道
Kotlin coordination channel
初识云原生安全:云时代的最佳保障