当前位置:网站首页>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:214. Shortest palindrome string
- [embedded] cortex m4f DSP Library
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- How to conduct interface test? What are the precautions? Nanny level interpretation
- LeetCode:498. Diagonal traversal
- LeetCode:41. 缺失的第一个正数
- Cesium draw points, lines, and faces
- 如何正确截取字符串(例:应用报错信息截取入库操作)
- TP-LINK enterprise router PPTP configuration
- LeetCode:39. 组合总和
猜你喜欢
Visual implementation and inspection of visdom
Detailed explanation of heap sorting
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Navicat Premium 创建MySql 创建存储过程
如何正确截取字符串(例:应用报错信息截取入库操作)
Tcp/ip protocol
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Generator parameters incoming parameters
LeetCode:236. 二叉树的最近公共祖先
[OC-Foundation框架]--<Copy对象复制>
随机推荐
LeetCode:124. 二叉树中的最大路径和
JVM quick start
TCP/IP协议
UML diagram memory skills
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
[embedded] cortex m4f DSP Library
Visual implementation and inspection of visdom
How to effectively conduct automated testing?
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Navicat Premium 创建MySql 创建存储过程
After reading the programmer's story, I can't help covering my chest...
TP-LINK 企业路由器 PPTP 配置
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
Tcp/ip protocol
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
Computer cleaning, deleted system files
[sword finger offer] serialized binary tree
marathon-envs项目环境配置(强化学习模仿参考动作)
[OC-Foundation框架]---【集合数组】
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置