题目:

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=;
if ( !root ) return ret;
int l = Solution::dfs4MaxSum(root->left, max_sum);
int r = Solution::dfs4MaxSum(root->right, max_sum);
if ( l> ) ret += l;
if ( r> ) 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 ;
int l = Solution::maxPS(root->left, maxSum);
int r = Solution::maxPS(root->right, maxSum);
maxSum = max(maxSum, root->val+(l>?l:)+(r>?r:));
return max(root->val,root->val+max(r,l));
}
};

【Binary Tree Maximum Path Sum】cpp的更多相关文章

  1. 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【Binary Tree Maximum Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,寻找值最大的路径. ...

  2. 【leetcode】Binary Tree Maximum Path Sum

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

  3. 【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 ...

  4. [leetcode]Binary Tree Maximum Path Sum

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

  5. 26. Binary Tree Maximum Path Sum

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

  6. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  7. LeetCode: Binary Tree Maximum Path Sum 解题报告

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

  8. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

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

  9. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

随机推荐

  1. 【风马一族_php】PHP与Mysql建立连接

    让php发出 Hi作为基础 http://www.cnblogs.com/sows/p/5990157.html 配置apache      ../apache/conf/httpd.conf 创建p ...

  2. 【SMS】移动短信网关返回信息状态代码说明【China Mobile】

    1 由SMSC返回的一般结果状态报告 含义 说明 处理建议DELIVRD 消息发送成功 用户成功接收到短信 ??EXPIRED 因为用户长时间关机或者不在服务区等导致的短消息超时没有递交到用户手机上 ...

  3. Lwip的相关资料

    文件资料 Lwip编程指南 Lwip协议栈的设计与实现 分析Lwip

  4. Linux使用Shell脚本实现ftp的自动上传下载

    1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...

  5. hive中的全排序

    写mapreduce程序时,如果reduce个数>1,想要实现全排序需要控制好map的输出 现在学了Hive,写sql大家都很熟悉,如果一个order by解决了全排序还用那么麻烦写mapred ...

  6. Incorrect column name 'productid '

    #1166 - Incorrect column name 'productid ' 解决方法:字段是复制的吧,复制的里面应该是有空格吧?检查一下,去掉就可以了哟,呵呵.  

  7. Material Design:CollapsingToolbarLayout

    activity_main.xml: <android.support.design.widget.CoordinatorLayout xmlns:android="http://sc ...

  8. 6)Java中String类

    1)String对象的初始化   由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下:     String s = “abc”;     ...

  9. Windows 8.1激活问题

    今天电脑开机,莫名的出现“你的Windows证书即将过期”. 系统本来用HEU_KMS_Activator 已激活成功,但查看系统激活状态时,结果如下图所示: 显示系统是已激活成功的.但还是有提示说系 ...

  10. ViewPager中GridView问题

    GridView 嵌套在ViewPager中问题. 1. GridView属性设置无法显示. 正常显示方式 <GridView android:padding="8dip" ...