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.

思路:存在val小于零的情况,所以path不一定是从叶子节点到叶子节点;

/**
* 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) {
maxPath = INT_MIN;
postOrderTraverse(root);
return maxPath;
} int postOrderTraverse(TreeNode* root){
if(root==NULL) return ;
int left, right, sum;
left = postOrderTraverse(root->left);
right = postOrderTraverse(root->right);
left = max(left,); //ignore negative value
right = max(right,);
sum = left + right +root->val;
if(sum > maxPath) maxPath = sum;
return root->val+max(left,right);//return current maximum sum from one child branch to root
}
private:
int maxPath;
};

124. Binary Tree Maximum Path Sum (Tree; DFS)的更多相关文章

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

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

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

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

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

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

  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 (DFS)

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

随机推荐

  1. goreman 多进程管理工具

    Linux下多进程管理工具对开发和运维都很有用,常见的功能全面的主流工具主要有monit.supervisor.不过开发中使用则推荐轻量级小工具goreman 举个例子:coreos的etcd就是使用 ...

  2. riotjs 简单使用&&browserify 构建

    项目地址: http://riotjs.com/ 备注: 为了简单使用了 browserify 进行构建 1. 项目结构 ├── app.css ├── gulpfile.js ├── index.h ...

  3. 去掉UIWebView上下滚动出边界时的黑色阴影

    for (UIView *viewin [_webViewsubviews]){ if ([viewisKindOfClass:[UIScrollView class]]){ for (UIView ...

  4. win7下安装ubuntu14.04lts 双系统

    首先,在win7下的硬盘管理 压缩出一块空闲的分区,即压缩卷之后,不做任何操作. 并且确保该空闲卷是“基本”类型     不是的话,参考http://www.jianshu.com/p/2f07312 ...

  5. Tomcat(64位)免安装版的环境安装与配置

    本篇博客主要介绍Tomcat(64位)免安装版的环境安装与配置,该篇文章同样适合于32位Tomcat免安装版的环境安装与配置. 该篇博客中的大部分内容同百度经验中的<出现unable to op ...

  6. Data_Structure01-绪论作业

    一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...

  7. Linux系统命令与脚本开发

    系统命令 # cat EFO cat >> file << EOF neirong EOF # 清空 >file 清空文件 [root@Poppy conf]# sed ...

  8. netcore中使用log4net日志

    第一.控制台程序中使用log4net  static void Main(string[] args) { ILoggerRepository repository = LoggerManager.C ...

  9. 字体相关CSS属性介绍

    font-family 字体系列. font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. 简单实例: bod ...

  10. Java Thread 多线程同步、锁、通信

    参看:http://www.cnblogs.com/hoojo/archive/2011/05/05/2038101.html