Binary Tree Maximum Path Sum
来源:互联网 发布:为什么4g网络特别慢 编辑:程序博客网 时间:2024/05/17 03:37
class Solution {
public:
int maxPathSum(TreeNode* root) {
if(root==nullptr) return 0;
int i=INT_MIN;
maxP(root,i);
return i;
{
if(!root) return 0;
int left=maxP(root->left,max1);
int right=maxP(root->right,max1);
int s=left+right+root->val; //单独一条路;
int s1=max((max(left,right)+root->val),root->val); //一条支路;
max1=max(max1,s); //max1只是用来存最大值;
return s1<=0?0:s1; //一定要判断,如果s1<=0 则舍弃;
}
public:
int maxPathSum(TreeNode* root) {
if(root==nullptr) return 0;
int i=INT_MIN;
maxP(root,i);
return i;
}
{
if(!root) return 0;
int left=maxP(root->left,max1);
int right=maxP(root->right,max1);
int s=left+right+root->val; //单独一条路;
int s1=max((max(left,right)+root->val),root->val); //一条支路;
max1=max(max1,s); //max1只是用来存最大值;
return s1<=0?0:s1; //一定要判断,如果s1<=0 则舍弃;
}
};
0 0
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 欢迎使用CSDN-markdown编辑器
- iOS开发之XCode快捷键
- 容器
- pyqt 5.5 for mac安装注意事项
- JAVA实现AES加密
- Binary Tree Maximum Path Sum
- 数据库三大范式
- innerHTML、innerText和outerHTML、outerText的区别
- 探索Android中的Parcel
- 字符设备程序---基础篇
- 1017. Queueing at Bank (25)
- Qt 事件处理机制 (下篇)
- poj2031Building a Space Station【空间坐标距离+prime】
- VIJOS 1212 Way Selection