当前位置:网站首页>LeetCode 124. Binary tree maximum path sum - binary tree series question 8
LeetCode 124. Binary tree maximum path sum - binary tree series question 8
2022-06-27 16:48:00 【CP Coding】
A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node's values in the path.
Given the root of a binary tree, return the maximum path sum of any non-empty path.
Example 1:

Input: root = [1,2,3] Output: 6 Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
Example 2:

Input: root = [-10,9,20,null,null,15,7] Output: 42 Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
Constraints:
- The number of nodes in the tree is in the range
[1, 3 * 104]. -1000 <= Node.val <= 1000
A path in a binary tree path Defined , A path is a sequence of nodes , Adjacent nodes are connected by an edge , Each node can only appear once in the path , The path does not have to go through the root node , It doesn't have to start or end with a leaf node . Path sum is the sum of all node values in the path . It is required to find the largest path in the tree and .
According to the above description of the path , We can know that for a node , The path with the maximum path sum passing through the node is one of the following paths :
1) Node itself , That is, the path has only one node
2) Node itself + The path with the largest sum from the left child node down
3) Node itself + The path with the largest sum from the right child node down
4) The path with the largest sum from the left child node down + Node itself + The path with the largest sum from the right child node down .
above 4 The path with the largest sum is the maximum sum path passing through that node , So find the maximum sum path of all nodes , Then find out the maximum path in the whole tree .
So we can use the following sequence to go through the calendar , When traversing a node , First, recursively call and wait for the left subtree to return the path with the maximum sum from the left subtree , Then recursively call and wait for the right subtree to return the path with the maximum sum from the right subtree , Then we can calculate the above described 4 The sum of the paths , Find the maximum sum . Finally, we have to put “ Node itself + The path with the largest sum from the left child node down ” and “ Node itself + The path with the largest sum from the right child node down ” The larger of is returned to the upper layer for use .
class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
maxSum = -1000
def helper(node):
if not node:
return 0
nonlocal maxSum
lsum = helper(node.left)
rsum = helper(node.right)
maxSum = max(maxSum, lsum + rsum + node.val)
res = max(max(lsum + node.val, rsum + node.val), node.val)
maxSum = max(maxSum, res)
return res
helper(root)
return maxSum边栏推荐
- 开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
- Open source 23 things shardingsphere and database mesh have to say
- About MySQL: the phenomenon and background of the problem
- How to modify / display GPIO status through ADB shell
- Unity 阴影——阴影平坠(Shadow pancaking)
- Oracle概念三
- Deeply digitise, lead cloud nativity and serve more developers
- Jialichuang EDA professional edition all offline client release
- Adaoracle supports multi chain distributed Oracle with wide area node quotation
- 3.1 simple condition judgment
猜你喜欢

Source NAT address translation and server mapping web page configuration of firewall Foundation

Open source 23 things shardingsphere and database mesh have to say

Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance

Leetcode daily practice (longest substring without repeated characters)

Redis Series 2: data persistence improves availability

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

米哈游起诉五矿信托,后者曾被曝产品暴雷

Event listening mechanism

Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)

Leetcode daily practice (main elements)
随机推荐
What is RPC
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
P.A.R.A 方法在思源的简易应用(亲测好用)
What is the level 3 password complexity of ISO? How often is it replaced?
3.3 one of the fixed number of cycles
LeetCode每日一练(两数之和)
About MySQL: the phenomenon and background of the problem
What should the ultimate LAN transmission experience be like
特殊函数计算器
继手机之后 报道称三星也削减了电视等家电产品线的产量
【多线程】线程通信调度、等待集 wait() 、notify()
Regular matching starts with what, ends with what, starts with what, and ends with what
分布式Session解决方案
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
Raspberry pie preliminary use
带你认识图数据库性能和场景测试利器LDBC SNB
Use redis to automatically cancel orders within 30 minutes
C language course design
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth