当前位置:网站首页>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边栏推荐
- 数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
- C语言教师工作量管理系统
- Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
- The role of the symbol @ in MySQL
- LeetCode每日一练(两数之和)
- SQL parsing practice of Pisa proxy
- Redis系列2:数据持久化提高可用性
- QT audio playback upgrade (7)
- Kubernetes基础自学系列 | Ingress API讲解
- Leetcode daily practice (longest substring without repeated characters)
猜你喜欢

Redis系列2:数据持久化提高可用性

Practice of constructing ten billion relationship knowledge map based on Nebula graph

Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm

Redis Series 2: data persistence improves availability

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮

继手机之后 报道称三星也削减了电视等家电产品线的产量

智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维

事件监听机制

当发布/订阅模式遇上.NET
随机推荐
当发布/订阅模式遇上.NET
Kubernetes基础自学系列 | Ingress API讲解
logstash排除特定文件或文件夹不采集上报日志数据
Leetcode daily practice (longest substring without repeated characters)
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
The role of the symbol @ in MySQL
面试半年,上个月成功拿到阿里P7offer,全靠我啃烂了这份2020最新面试题!
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
[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
About MySQL: the phenomenon and background of the problem
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
A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe
树莓派初步使用
C language course design
Data center table reports realize customized statistics, overtime leave summary record sharing
C language set operation
C语言课程设计
Hierarchical clustering and case analysis
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
Source NAT address translation and server mapping web page configuration of firewall Foundation