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

```       cur
/    \
left  right```

```class Solution {
public:
int maxPathSum(TreeNode *root) {
if(root == NULL)
;
int maxsum = INT_MIN;
int ret = getMax(root, maxsum);
return max(ret, maxsum);

}

int getMax(TreeNode *root, int &maxsum)
{
if(root == NULL)
;
int leftmax = getMax(root->left, maxsum);
int rightmax = getMax(root->right, maxsum);
maxsum = max(maxsum, max(max(leftmax, rightmax), leftmax + root->val + rightmax));
return max(root->val, max(leftmax, rightmax) + root->val);

}
};```

## LeetCode124: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] 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. ...

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

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

7. [lintcode] Binary Tree Maximum Path Sum II

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

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

2016-12-17  21:10:28 吉祥物:Duke(公爵)    Logo:咖啡(爪哇岛盛产咖啡)  An overview of the software development proce ...

2. Android：控件AutoCompleteTextView 自动提示

在文本框中输入,要这样的提示效果,如果你输入的是aac,在输入aa后,选择aac,文本框的内容会自动补齐,输入aac(类似百度搜索文本框的显示结果)   <AutoCompleteTextVie ...

3. opencv 比较直方图方式 进行人脸检测对比

完整opencv(emgucv)人脸.检测.采集.识别.匹配.对比 //成对几何直方图匹配               public static string MatchHist()         ...

4. 关于过拟合、局部最小值、以及Poor Generalization的思考

Poor Generalization 这可能是实际中遇到的最多问题. 比如FC网络为什么效果比CNN差那么多啊,是不是陷入局部最小值啊?是不是过拟合啊?是不是欠拟合啊? 在操场跑步的时候,又从SVM ...

5. 最全的运营推广方案，教你如何从零开始运营APP

转载自:最全的运营推广方案,教你如何从零开始运营APP(http://www.kusocial.com/9319 ) 最全的运营推广方案,教你如何从零开始运营APP 2014-10-07 • 运营 • ...

6. [原创]Lodop打印, 以及Lodop引用css文件控制打印样式的问题.

最近在做Lodop打印功能: 思路是:  用MasterPage搭个打印页面的框架, 然后在具体的页面中填入数据, 打印的样式由母版页和CSS来控制. 困扰了一天的问题是:  在打印的JS文件中, 引 ...

7. EasyUi&ndash;7.tab和datagrid和iframe的问题

1. 多个tab切换,第2个不显示 动态添加tab Iframe页面的方法 展开 折叠 <script type="text/javascript"> \$(functi ...

8. 使用虚幻引擎中的C++导论（二-UE4基类）

使用虚幻引擎中的C++导论(二) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...

9. JDK环境变量的配置方法

1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 变量名:JAVA_HOME 变量值:jdk的目录,比如d:/java 3. 选择“系统变量”中变量名为“Path”的环 ...

10. ANDROID开发之SQLite详解

本文转自:http://www.cnblogs.com/Excellent/archive/2011/11/19/2254888.html