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. select点击option获取文本输入框的焦点事件

    HTML文件: <select id="secOrderNum" style="margin-bottom:10px;width:90px;" data- ...

  2. 【BZOJ】【1968】【AHOI2005】COMMON 约数研究

    数论 原谅我这么傻逼的题都不会做…… 或许写成数学公式的形式比较容易想到解法? $$ans=\sum_{i=1}^n \sum_{d|i} 1$$ ……是不是感觉很水呀……是吧……改成先枚举d再枚举 ...

  3. iOS:删除、插入、移动单元格

    删除.插入.移动单元格的具体实例如下:   代码如下: #import "ViewController.h" #define NUM 20 typedef enum { delet ...

  4. JavaScript事件代理和事件委托

    一.概述: 那什么叫事件委托呢?它还有一个名字叫事件代理,JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件.那这是什么意思呢?网上的 ...

  5. POJ 2488 A Knight&#39;s Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29226   Accepted: 10 ...

  6. Android -- 触摸Area对焦区域(更新)

    老早就想找关于不同点击不同地方的对焦,但是一直没有找到,现在项目又需要这个功能,又跑出来找找,最后还是找到啦~~关于对焦更多的是关于自动对焦. 废话不多说,直接来干货,主要是setFocusAreas ...

  7. 在myeclipse中写sql语句的细节问题

    注意类型,varchar 和int  在java中表示为sql语句中的细微区别!! 下面的REGISEAT_NUM为int 类型       custid为varchar类型 String sql1= ...

  8. libsvm svmtrain函数运行出错问题

    我安装的是matlab R2013a   安装上libsvm后需要设置set path 为libsvm的所有文件夹加载上,但是发现每次重新打开之后,之前加载的又不见了,所有导致出现,使用svmsrai ...

  9. yeoman-angular-gulp

    1.安装 yeoman npm install -g yo gulp bower 2.安装genarate-angular //npm install generator-angular npm in ...

  10. CSS半透明兼容写法

    filter: Alpha(opacity=10); -moz-opacity:0.1; opacity:0.1; 例如: background:#A5CD40; filter: Alpha(opac ...