# 题目

Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.

For example:
Given the below binary tree,

```       1
/ \
2   3
```

Return `6`.

# 分析

F(left) + F(right) + val...当然如果left,或者right<0就不用加了的= =

# 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 maxVal = INT_MIN;
int maxPathSum(TreeNode* root) {
if (root == NULL)
;

maxSum(root);
return maxVal;
}

/*递归函数*/
int maxSum(TreeNode *root)
{
if (root == NULL)
;

/*求以root为根的当前子树的最大路径和*/
int curVal = root->val;
int lmaxSum = maxSum(root->left), rmaxSum = maxSum(root->right);
)
curVal += lmaxSum;
)
curVal += rmaxSum;

if (curVal > maxVal)
maxVal = curVal;

/*返回以当前root为根的子树的最大路径和*/
return max(root->val, max(root->val + lmaxSum, root->val + rmaxSum));
}
};```

## LeetCode（124） Binary Tree Maximum Path Sum的更多相关文章

1. [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 ...

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

4. leetcode 124. Binary Tree Maximum Path Sum

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

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

6. leetcode 124. Binary Tree Maximum Path Sum ----- java

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

7. leetcode&ndash;Binary Tree Maximum Path Sum

1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

8. 【leetcode】Binary Tree Maximum Path Sum (medium)

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

9. [Leetcode][JAVA] 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. ...

## 随机推荐

1. java在线支付

http://blog.csdn.net/lidew521/article/category/1437251

2. 关于sql的执行顺序

标准sql的解析顺序为 1)FROM子句,组装来自不同数据源的数据 2)WHERE子句 基于制定的条件对记录进行筛选 3)GROUP BY 子句将数据划分为多个分组 4)使用聚合函数进行计算 5) 使 ...

3. 把文件打成zip或然rar下载 （详询请加qq：2085920154）

4. laravel中如何写验证码文件,并防止中文乱码.

本例为生成中文验证码,专为laravel而生. //控制器: public function getVcode(Request \$request){ \$width=845; \$height=125; ...

5. WinRAR注册

新建一个txt文件并命名为"rarreg.key",添加以下内容保存,然后放置在WinRAR安装目录: RAR registration data Federal Agency f ...

6. poj1144 求不同割点的个数

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11914   Accepted: 5519 Descript ...

7. img图片放大控件 lightbox.js

1.引入相关js,css jquery-1.7.2.min.js,lightbox.css,lightbox.js   2 图片增加<a href="大图片地址" rel=& ...

8. BZOJ4298 : [ONTAK2015]Bajtocja

设f[i][j]为第i张图中j点所在连通块的编号,加边时可以通过启发式合并在\$O(dn\log n)\$的时间内维护出来. 对于每个点,设h[i]为f[j][i]的hash值,若两个点hash值相等,则 ...

9. Spring-JDBC通用Dao

JdbcBaseDao JdbcBaseDao接口,内容如下: package com.sun4j.core.jdbc.dao; import java.io.Serializable; import ...

10. zookeeper数据迁移

在不停机的情况下,实现集群之间数据迁移代码: private void create(ZooKeeper zk1, ZooKeeper zk2, String path) throws Excepti ...