当前位置:网站首页>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);
}
};
边栏推荐
- LeetCode:836. 矩形重叠
- LeetCode:劍指 Offer 42. 連續子數組的最大和
- Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
- Trying to use is on a network resource that is unavailable
- Mongodb installation and basic operation
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
- [OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
- LeetCode:387. 字符串中的第一个唯一字符
- [embedded] print log using JLINK RTT
猜你喜欢
随机推荐
The network model established by torch is displayed by torch viz
Bitwise logical operator
深度剖析C语言指针
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
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
Screenshot in win10 system, win+prtsc save location
LeetCode:221. 最大正方形
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
vb. Net changes with the window, scales the size of the control and maintains its relative position
ESP8266-RTOS物联网开发
Double pointeur en langage C - - modèle classique
The mysqlbinlog command uses
Navicat premium create MySQL create stored procedure
[OC-Foundation框架]---【集合数组】
Cesium draw points, lines, and faces
Restful API design specification
Computer cleaning, deleted system files
UML圖記憶技巧
Problems encountered in connecting the database of the project and their solutions









