当前位置:网站首页>LeetCode:124. Maximum path sum in binary tree
LeetCode:124. Maximum path sum in binary tree
2022-07-06 08:51:00 【Bertil】
route It is defined as a path starting from any node in the tree , Along the parent node - Child node connection , A sequence that reaches any node . The same node in a path sequence At most once . This path At least one node , And it doesn't have to go through the root node .
Path and Is the sum of the values of the nodes in the path .
Give you the root node of a binary tree root , Back to its The maximum path and .
Example 1:

Input :root = [1,2,3]
Output :6
explain : The best path is 2 -> 1 -> 3 , Path and are 2 + 1 + 3 = 6
Example 2:
Input :root = [-10,9,20,null,null,15,7]
Output :42
explain : The best path is 15 -> 20 -> 7 , Path and are 15 + 20 + 7 = 42
Tips :
- The range of nodes in the tree is [1, 3 * 10^4]
- -1000 <= Node.val <= 1000
Their thinking
1. First, traverse the depth of the left and right subtrees , The maximum value of the current node can be divided into three cases :
- Go to the left : Current node +L( The maximum depth of the left subtree )
- Go to the right : Current node +R( The maximum depth of the right subtree )
- Don't go : If the current node is negative , Go straight back to 0
2. Then define the maximum path and ans Root node + The left subtree + Maximum depth of right subtree
3. Last , After recursion, return the maximum path and ans that will do
Code
/** * 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;
// Go down from the current node
function dfs(root) {
if(!root) return 0;
// Traversing the depth of the left subtree
let left = dfs(root.left);
// Traverse the depth of the right subtree
let right = dfs(root.right);
// The maximum value of the update path
ans = Math.max(ans, root.val + left + right);
// Return the maximum value from the current node
return Math.max(0, Math.max(left, right) + root.val);
}
};
边栏推荐
- View computer devices in LAN
- Nacos 的安装与服务的注册
- LeetCode:劍指 Offer 42. 連續子數組的最大和
- Revit secondary development Hof method calls transaction
- After reading the programmer's story, I can't help covering my chest...
- 如何正确截取字符串(例:应用报错信息截取入库操作)
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- PC easy to use essential software (used)
- [embedded] print log using JLINK RTT
- 如何进行接口测试测?有哪些注意事项?保姆级解读
猜你喜欢

C語言雙指針——經典題型

Screenshot in win10 system, win+prtsc save location

Deep anatomy of C language -- C language keywords

Nacos 的安装与服务的注册

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

多元聚类分析

704 binary search

Light of domestic games destroyed by cracking

可变长参数

Restful API design specification
随机推荐
使用latex导出IEEE文献格式
Revit secondary development Hof method calls transaction
LeetCode:39. Combined sum
pytorch查看张量占用内存大小
LeetCode:41. 缺失的第一个正数
Deep analysis of C language data storage in memory
有效提高软件产品质量,就找第三方软件测评机构
Unsupported operation exception
项目连接数据库遇到的问题及解决
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
LeetCode:剑指 Offer 03. 数组中重复的数字
Li Kou daily question 1 (2)
Nacos 的安装与服务的注册
Current situation and trend of character animation
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
【嵌入式】使用JLINK RTT打印log
Mongodb installation and basic operation
多元聚类分析
LeetCode:236. The nearest common ancestor of binary tree