当前位置:网站首页>LeetCode:124. 二叉树中的最大路径和
LeetCode:124. 二叉树中的最大路径和
2022-07-06 08:44:00 【Bertil】
路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。
路径和 是路径中各节点值的总和。
给你一个二叉树的根节点 root ,返回其 最大路径和 。
示例 1:

输入:root = [1,2,3]
输出:6
解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
示例 2:
输入:root = [-10,9,20,null,null,15,7]
输出:42
解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
提示:
- 树中节点数目范围是 [1, 3 * 10^4]
- -1000 <= Node.val <= 1000
解题思路
1.首先遍历左右子树的深度,当前节点往下走的最大值分三种情况:
- 往左走:当前节点+L(左子树的最大深度)
- 往右走:当前节点+R(右子树的最大深度)
- 不走:当前节点如果为负值,直接返回0
2.然后定义最大路径和ans为根节点+左子树+右子树的深度最大值
3.最后,递归完成后直接返回最大路径和ans即可
代码
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/** * @param {TreeNode} root * @return {number} */
var maxPathSum = function(root) {
let ans = -9999;
dfs(root);
return ans;
// 从当前节点往下走
function dfs(root) {
if(!root) return 0;
// 遍历左子树的深度
let left = dfs(root.left);
// 遍历右子树的深度
let right = dfs(root.right);
// 更新路径的最大值
ans = Math.max(ans, root.val + left + right);
// 返回从当前节点往下走的最大值
return Math.max(0, Math.max(left, right) + root.val);
}
};
边栏推荐
- 【MySQL】日志
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- 【ROS】usb_cam相机标定
- Purpose of computer F1-F12
- Indentation of tabs and spaces when writing programs for sublime text
- [MySQL] log
- Generator parameters incoming parameters
- JS pure function
- 电脑清理,删除的系统文件
- Unified ordering background interface product description Chinese garbled
猜你喜欢

个人电脑好用必备软件(使用过)

Promise 在uniapp的简单使用

Screenshot in win10 system, win+prtsc save location

Precise query of tree tree

2022.02.13 - NC001. Reverse linked list
![[MySQL] lock](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
[MySQL] lock

【ROS】usb_ Cam camera calibration

JVM performance tuning and practical basic theory - Part 1

【MySQL】锁

What is CSRF (Cross Site Request Forgery)?
随机推荐
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
Excellent software testers have these abilities
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
JVM 快速入门
生成器参数传入参数
Process of obtaining the electronic version of academic qualifications of xuexin.com
Browser thread
China polyether amine Market Forecast and investment strategy report (2022 Edition)
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Delay initialization and sealing classes
Trying to use is on a network resource that is unavailable
移位运算符
2022.02.13 - NC002. sort
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
JS native implementation shuttle box
【嵌入式】使用JLINK RTT打印log
Current situation and trend of character animation
Generator parameters incoming parameters
Unsupported operation exception