当前位置:网站首页>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);
}
};
边栏推荐
- Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- 游戏解包的危害及资源加密的重要性
- ROS编译 调用第三方动态库(xxx.so)
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- Function coritization
- Computer cleaning, deleted system files
- [cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
- [MySQL] log
猜你喜欢
深度剖析C语言数据在内存中的存储
Computer cleaning, deleted system files
The harm of game unpacking and the importance of resource encryption
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
TP-LINK 企业路由器 PPTP 配置
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
【MySQL】鎖
Crash problem of Chrome browser
egg. JS project deployment online server
Verrouillage [MySQL]
随机推荐
Research and investment forecast report of citronellol industry in China (2022 Edition)
pytorch训练好的模型在加载和保存过程中的问题
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 camera calibration
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
Browser thread
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Promise 在uniapp的简单使用
win10系统中的截图,win+prtSc保存位置
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
torch建立的网络模型使用torchviz显示
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
Deep analysis of C language data storage in memory
MySQL learning record 10getting started with JDBC
2022.02.13 - NC001. Reverse linked list
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
sublime text的编写程序时的Tab和空格缩进问题
【MySQL】锁
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
swagger设置字段required必填