https://leetcode.com/problems/binary-tree-maximum-path-sum/

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.

/**
* 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 res = INT_MIN; int dfs(TreeNode* root) {
if(!root) return ; int l = dfs(root->left);
int r = dfs(root->right); int rhs = root->val;
if(l > ) rhs += l;
if(r > ) rhs += r;
res = max(res, rhs); return max(l, r) <= ? root->val: max(l, r) + root->val;
} int maxPathSum(TreeNode* root) {
if(!root) return ; dfs(root); return res;
}
};

leetcode@ [124] Binary Tree Maximum Path Sum (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 求二叉树的最大路径和

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

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

  5. Java for LeetCode 124 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. ...

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

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

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

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

随机推荐

  1. Unity3D开发(一):NGUI之UIRoot屏幕分辨率自适应

    原地址:http://blog.csdn.net/onerain88/article/details/11713299 NGUI在Unity3D游戏开发中非常常用,而NGUI对于每一个UI场景,都是以 ...

  2. 在ECLIPSE中用MAVEN和TOMCAT来建立WEBAPP

    找了很多示例,结合以下两个URL,比较简单的测试了一下. http://blog.csdn.net/clj198606061111/article/details/20221133 http://ww ...

  3. 【BZOJ 2829】 2829: 信用卡凸包 (凸包)

    2829: 信用卡凸包 Description Input Output Sample Input 2 6.0 2.0 0.0 0.0 0.0 0.0 2.0 -2.0 1.5707963268 Sa ...

  4. PLS-00215:字符串长度限制在范围

    在Oracle中有一张people表 创建跟新表的存储过程 修改定义字段长度 总结:在Oracle中执行存储过程时,输出参数的长度要与原表中字段长度一致!

  5. 再分析 返回值加引用&,const

    本文主要分析,返回&,和返回值加const的作用. 返回& 定义一个数组模板: template<class T>class Array{ enum{size = 100} ...

  6. MS SQL SERVER: msdb.dbo.MSdatatype_mappings & msdb.dbo.sysdatatypemappings

    --SQL转Oracle/DB2的类型对应关系SELECT *FROM msdb.dbo.MSdatatype_mappings; --MS SQL SERVER更详细得显示了ORACLE/DB2各个 ...

  7. 使用net start mysql的时候出现服务名无效的原因及解决办法

    原因:mysql服务没有安装 解决办法:使用管理员权限,执行mysqld -install命令 然后以管理员身份net start mysql开启mysql服务 卸载mysql服务的方法 1.管理员权 ...

  8. UML中关系图解

    转自http://blog.csdn.net/duran1986/article/details/5573415 最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有 ...

  9. mappingResources,annotatedClasses(映射)

    这两个是有本质区别的,光看名字也能看出来,哈哈,好了,入正题: mappingResources用于指定少量的hibernate配置文件像这样 Xml代码 1 2 3 4 5 6 7 <prop ...

  10. 重启php-fpm

    查看pid位置 cat /etc/php-fpm.conf pid = /usr/local/php/var/run/php-fpm.pid #测试php-fpm配置/usr/local/php/sb ...