当前位置:网站首页>High frequency leetcode deep search part: 124 Maximum path sum in binary tree
High frequency leetcode deep search part: 124 Maximum path sum in binary tree
2022-07-22 09:28:00 【Hiccup~~~~】
124. The maximum path in a binary tree and
Difficulty 1426
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 * 104]
- -1000 <= Node.val <= 1000
Ideas
- Failure thinking : In the sequence traversal , Set global variables , Accumulate during traversal ( Path selection is not considered : Up or right )
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
private:
int maxSum = INT_MIN;
public:
int maxGain(TreeNode* node) {
if (node == nullptr) {
return 0;
}
// Recursively calculate the maximum contribution value of left and right child nodes
// Only when the maximum contribution value is greater than 0 when , The corresponding child node will be selected
int leftGain = max(maxGain(node->left), 0);
int rightGain = max(maxGain(node->right), 0);
// The maximum path sum of a node depends on the value of the node and the maximum contribution value of its left and right children ( Left middle right )
int priceNewpath = node->val + leftGain + rightGain;
// Update answers
maxSum = max(maxSum, priceNewpath);
// Returns the maximum contribution value of the node ,( Continue to find the parent node )
return node->val + max(leftGain, rightGain);
}
int maxPathSum(TreeNode* root) {
maxGain(root);
return maxSum;
}
};
边栏推荐
- 嵌入式之认识网卡phy自协商
- GAN目标函数理解
- 如何快速开发一个简单实用的MES系统?
- c语言之指针(二)
- Tens of millions of data: from 2000 to 2019, enterprise registration data longitude and latitude, registration number and other multi indicator information of provinces, cities and counties in China
- c语言之指针(四)
- The multi indicator data of 4000+ listed companies' provinces, cities and operations have been updated to 2022.1
- h5py快速入门指南
- 修改Hosts文件自定义本机IP域名
- 高频leetcode深搜部分:695. 岛屿的最大面积
猜你喜欢
随机推荐
c语言之指针(一)
嵌入式之认识网卡phy自协商
嵌入式之SD卡/U盘只读问题解决方案(FAT只读修复方式)
飞机大战分析及代码
设备重启卡死问题分析-reboot卡死
GAN目标函数理解
飞机躲子弹小游戏案例
Exception class
I have written the experience of loan system for 2 months
dtcloud 使用自定义字体
高频leetcode深搜部分:剑指 Offer 13. 机器人的运动范围
Small game cases of aircraft dodging bullets
1989-2020年学历结构和计算平均受教育年限数据
高频leetcode深搜部分:剑指 Offer 36. 二叉搜索树与双向链表
排序方法--冒泡排序(使用数组实现一串数字的从大到小或从小到大排序)
嵌入式之网络问题总结(网卡丢包、网卡无法识别)
core technology
Literature: case "film ranking"
Thread多线程
修改Hosts文件自定义本机IP域名