当前位置:网站首页>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);
}
};
边栏推荐
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
- [OC-Foundation框架]-<字符串And日期与时间>
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Super efficient! The secret of swagger Yapi
- 如何正确截取字符串(例:应用报错信息截取入库操作)
- To effectively improve the quality of software products, find a third-party software evaluation organization
- Revit secondary development Hof method calls transaction
- Visual implementation and inspection of visdom
- 可变长参数
猜你喜欢

MySQL uninstallation and installation methods

Deep analysis of C language data storage in memory
![[OC]-<UI入门>--常用控件的学习](/img/2c/d317166e90e1efb142b11d4ed9acb7.png)
[OC]-<UI入门>--常用控件的学习
![[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born](/img/70/d275009134fcbf9ae984c0f278659e.jpg)
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born

Image, CV2 read the conversion and size resize change of numpy array of pictures

项目连接数据库遇到的问题及解决

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

【嵌入式】使用JLINK RTT打印log

Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)

Alibaba cloud server mining virus solution (practiced)
随机推荐
有效提高软件产品质量,就找第三方软件测评机构
poi追加写EXCEL文件
Mongodb installation and basic operation
LeetCode:221. 最大正方形
Tcp/ip protocol
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Leetcode: Sword finger offer 48 The longest substring without repeated characters
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
超高效!Swagger-Yapi的秘密
【剑指offer】序列化二叉树
Generator parameters incoming parameters
Deep anatomy of C language -- C language keywords
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
Double pointeur en langage C - - modèle classique
Bitwise logical operator
LeetCode:41. 缺失的第一个正数
TCP/IP协议
Indentation of tabs and spaces when writing programs for sublime text
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
LeetCode:剑指 Offer 04. 二维数组中的查找