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.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

树结构显然用递归来解,解题关键:

1、对于每一层递归,只有包含此层树根节点的值才可以返回到上层。否则路径将不连续。

2、返回的值最多为根节点加上左右子树中的一个返回值,而不能加上两个返回值。否则路径将分叉。

在这两个前提下有个需要注意的问题,最上层返回的值并不一定是满足要求的最大值,

因为最大值对应的路径不一定包含root的值,可能存在于某个子树上。

因此解决方案为设置全局变量maxSum,在递归过程中不断更新最大值。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxSum;
Solution()
{
maxSum = INT_MIN;
}
int maxPathSum(TreeNode* root)
{
Helper(root);
return maxSum;
}
int Helper(TreeNode *root) {
if(!root)
return INT_MIN;
else
{
int left = Helper(root->left);
int right = Helper(root->right);
if(root->val >= )
{//allways include root
if(left >= && right >= )
maxSum = max(maxSum, root->val+left+right);
else if(left >= && right < )
maxSum = max(maxSum, root->val+left);
else if(left < && right >= )
maxSum = max(maxSum, root->val+right);
else
maxSum = max(maxSum, root->val);
}
else
{
if(left >= && right >= )
maxSum = max(maxSum, max(root->val+left+right, max(left, right)));
else if(left >= && right < )
maxSum = max(maxSum, left);
else if(left < && right >= )
maxSum = max(maxSum, right);
else
maxSum = max(maxSum, max(root->val, max(left, right)));
}
//return only one path, do not add left and right at the same time
return max(root->val+max(, left), root->val+max(, right));
}
}
};

【LeetCode】124. Binary Tree Maximum Path Sum的更多相关文章

  1. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  2. Leetcode solution 124: Binary Tree Maximum Path Sum

    Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path ...

  3. 【Lintcode】094.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 tr ...

  4. 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 ...

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

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

  6. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  7. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  8. [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  9. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

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

随机推荐

  1. [Linux] Systemd 入门教程:命令篇

    reference : http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html Systemd 是 Linux 系统 ...

  2. Coursera课程python中的一些程序

    Index of /code Name Last modified Size Description Parent Directory - BeautifulSoup.py 07-Aug-2015 1 ...

  3. Asp.Net Core App 部署故障示例 1

    相关阅读:Windows + IIS 环境部署Asp.Net Core App 1.  HTTP Error 502.5 – Process Failure 环境 Windows Server 201 ...

  4. 【BZOJ】【2502】清理雪道

    网络流/上下界网络流 带下界的最小可行流…… 我SB了,跑网络流的时候是得从虚拟源0往出跑……而不是S…… Orz Hzwer /*********************************** ...

  5. Java JDBC数据库链接

    好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC. 1.使用Java JDBC操作数据库一般需要6步: (1)建立JDBC桥接器,加载数据库驱动: (2)连接数据库,获得Connect ...

  6. DP 换硬币问题

    设有n种不同面值的硬币,现要用这些面值的硬币来找开待凑钱数m,可以使用的各种面值的硬币个数不限.    找出最少需要的硬币个数,并输出其币值. package DP; import java.util ...

  7. scala 学习笔记四 匿名函数

    1.介绍 Scala 中定义匿名函数的语法很简单,箭头左边是参数列表,右边是函数体. 使用匿名函数后,我们的代码变得更简洁了. 下面的表达式就定义了一个接受一个Int类型输入参数的匿名函数: var ...

  8. PREEMPT_RT的未来

    因为开发资金的问题,Thomas Gleixner宣布他已经不想干了. 商业公司往往用了PREEMPT_RT的功能去不愿意去回报社区,那就自己弄吧. http://lwn.net/Articles/6 ...

  9. Discuz常见大问题-如何开启和使用首页四格

    在论坛-首页四格中,勾选开启首页四格,然后可以选择数据来源的板块 注意首页四格刷新是有时间的,5分钟左右,不是你这里更新完了帖子那里就有了(如果你自己建的网站,可能回复和热帖都还没有) 当然你也可以使 ...

  10. lenovo E431 win8系统 安装win7

    事由:新买的lenovo e431 原装win8系统.win8系统本来就不好用,又傻傻的装了win10的预览版,丑死不说,还各种驱动不兼容,每次开机都提示不兼容——本人有点洁癖,见不得这种,本想把不兼 ...