当前位置:网站首页>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);
}
};
边栏推荐
- Revit secondary development Hof method calls transaction
- Nacos 的安装与服务的注册
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- C语言深度解剖——C语言关键字
- Hutool gracefully parses URL links and obtains parameters
- TDengine 社区问题双周精选 | 第三期
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- @JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
- hutool优雅解析URL链接并获取参数
- poi追加写EXCEL文件
猜你喜欢
随机推荐
Li Kou daily question 1 (2)
LeetCode:41. 缺失的第一个正数
Light of domestic games destroyed by cracking
Bitwise logical operator
vb. Net changes with the window, scales the size of the control and maintains its relative position
Trying to use is on a network resource that is unavailable
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
【剑指offer】序列化二叉树
Double pointeur en langage C - - modèle classique
Philosophical enlightenment from single point to distributed
[OC]-<UI入门>--常用控件的学习
Charging interface docking tutorial of enterprise and micro service provider platform
LeetCode:836. Rectangle overlap
Sublime text using ctrl+b to run another program without closing other runs
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
LeetCode:剑指 Offer 04. 二维数组中的查找
[MySQL] limit implements paging
Problems encountered in connecting the database of the project and their solutions
Nacos 的安装与服务的注册
poi追加写EXCEL文件