当前位置:网站首页>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);
}
};
边栏推荐
- ROS compilation calls the third-party dynamic library (xxx.so)
- Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
- Cesium draw points, lines, and faces
- LeetCode:124. 二叉树中的最大路径和
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- Mongodb installation and basic operation
- MySQL uninstallation and installation methods
- 如何有效地进行自动化测试?
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- LeetCode:836. 矩形重叠
猜你喜欢
[OC-Foundation框架]---【集合数组】
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
深度剖析C语言数据在内存中的存储
MongoDB 的安装和基本操作
Current situation and trend of character animation
opencv+dlib实现给蒙娜丽莎“配”眼镜
Export IEEE document format using latex
Mongodb installation and basic operation
[embedded] cortex m4f DSP Library
Detailed explanation of dynamic planning
随机推荐
C语言深度解剖——C语言关键字
【剑指offer】序列化二叉树
LeetCode:673. Number of longest increasing subsequences
Deep analysis of C language data storage in memory
[NVIDIA development board] FAQ (updated from time to time)
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
Deep anatomy of C language -- C language keywords
【ROS】usb_ Cam camera calibration
After reading the programmer's story, I can't help covering my chest...
UML圖記憶技巧
使用latex导出IEEE文献格式
Leetcode: Sword finger offer 48 The longest substring without repeated characters
MYSQL卸载方法与安装方法
[OC-Foundation框架]---【集合数组】
Leetcode: Jianzhi offer 03 Duplicate numbers in array
Excellent software testers have these abilities
Navicat premium create MySQL create stored procedure
[OC]-<UI入门>--常用控件-UIButton
marathon-envs项目环境配置(强化学习模仿参考动作)
LeetCode:39. Combined sum