题目:

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:
Given the below binary tree,

       1/ \2   3

Return 6.

代码:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxPathSum(TreeNode* root){int max_sum = INT_MIN;Solution::dfs4MaxSum(root, max_sum);return max_sum;}static int dfs4MaxSum(TreeNode* root, int& max_sum){int ret=0;if ( !root ) return ret;int l = Solution::dfs4MaxSum(root->left, max_sum);int r = Solution::dfs4MaxSum(root->right, max_sum);if ( l>0 ) ret += l;if ( r>0 ) ret += r;ret += root->val;max_sum = std::max(ret, max_sum);return std::max(root->val, std::max(root->val+l, root->val+r));}
};

tips:

求array最大子序列和是一样的思路。只不过binary tree不是一路,而是分左右两路,自底向上算。

主要注意的地方如下:

1. 维护一个全局最优变量(max_sum),再用ret存放包括前节点在内的局部最大值;每次比较max_sum和ret时,max_sum代表的是不包含当前节点在内的全局最优,ret代表的是一定包含当前节点在内的全局最优。

2. dfs4MaxSum返回时需要注意,left和right只能算一路,不能一起都返到上一层。因为题目中要求的path抻直了只能是一条线,不能带分叉的。

==============================================

第二次过这道题,大体思路有了,细节的错误也都犯了,改过来强化一次AC了。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxPathSum(TreeNode* root){int ret = INT_MIN;if (root) Solution::maxPS(root, ret);return ret;}static int maxPS( TreeNode* root, int& maxSum){if ( !root ) return 0;int l = Solution::maxPS(root->left, maxSum);int r = Solution::maxPS(root->right, maxSum);maxSum = max(maxSum, root->val+(l>0?l:0)+(r>0?r:0));return max(root->val,root->val+max(r,l));}
};

转载于:https://www.cnblogs.com/xbf9xbf/p/4511277.html

【Binary Tree Maximum Path Sum】cpp相关推荐

  1. 【LeetCode】124. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  2. 【重点】LeetCode 124. Binary Tree Maximum Path Sum

    LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...

  3. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum 题目链接:https://leetcode.com/problems... dfs对每个node,查一下包含这个node的最大路径值. /** ...

  4. 124 Binary Tree Maximum Path Sum

    题目: 124 Binary Tree Maximum Path Sum 这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可 class Solution:def __ini ...

  5. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  6. 124. Binary Tree Maximum Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

  7. Leetcode: Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.Fo ...

  8. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

  9. leetcode @python 124. Binary Tree Maximum Path Sum

    题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...

最新文章

  1. 与孩子一起学编程 python_【和孩子一起学编程】 python笔记--第五天
  2. Shell脚本入门基础
  3. 【论文解读】ICDM2020 | 挖掘异构图中的层级结构
  4. 姑娘,你为什么要编程?
  5. 离散数学图论旅行规划问题_2020年MathorCup高校数学建模挑战赛——C 题 仓内拣货优化问题...
  6. linux(windows,mac)下安装ta-lib
  7. 1071-数字的空洞
  8. java删除文件夹及其内部文件
  9. BIM+GIS工程管理系统——BIM与GIS的跨界合作
  10. NVIDIA NGC镜像使用笔记
  11. HTML5 新特性!
  12. 个人信息保护建设实践方法的探索过程
  13. 驭梦KTV点歌系统简介
  14. 【CSP-J】【图论】【最短路】加工零件
  15. TCP与UDP协议,socket套接字编程,通信相关操作
  16. 远程工作的五个层次 – 以及为什么您可能处于第2层
  17. Spring-拦截器与过滤器
  18. dwz php 联动,PHP利用DWZ.CN服务生成短网址
  19. 显示器的 VGA、HDMI、DVI 和 DisplayPort
  20. 谷胱甘肽过氧化物酶(GSH-Px)4种不同的GSH-Px介绍

热门文章

  1. python3 beautifulsoup 表格_[Python3爬虫]Beautiful Soup解析库
  2. Quartus II14.1安装教程
  3. 光流 | 基于Matlab实现Lucas-Kanade方法:方法1(附源代码)
  4. 北斗导航 | 惯性导航之基于Matlab的IMU与GPS融合(附源代码)
  5. erlang精要(16)-匿名函数之sigmoid函数实现
  6. 趣学python3(38)--多项式最小二乘法拟合
  7. 聊一聊转行推荐的问题
  8. 【NLP】业界总结 | BERT的花式玩法
  9. 【数据挖掘】特征相关性挖掘神器-线性非线性关系一键挖掘!
  10. 《机器学习》课程视频(数据处理、模型构建与优化)