题目:

链接

解答:

自底向上求解。left_max right_max分别返回了左右子树的最大路径和,假设左右子树最大路径和小于0。那么返回零。 用这个最大路径和和根节点的值相加。来更新最大值,同一时候。 更新返回该树的最大路径值。

代码:

 class Solution {
public:
int max = INT_MIN;
int maxPathSum(TreeNode *root) {
if (root == NULL)
return 0;
search(root);
return max;
}
int search(TreeNode *root)
{
if (root == NULL)
return 0;
int left_max = search(root->left);
int right_max = search(root->right);
int sum = left_max + right_max + root->val;
if (sum > max)
max = sum;
sum = left_max > right_max ? left_max + root->val : right_max + root->val;
if (sum > 0)
return sum;
return 0;
} };

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 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

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

  7. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

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

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

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

随机推荐

  1. Oracle中的执行计划

    使用autotrace sqlplus系统参数:SQL> set autotrace trace onSQL> select * from dual;DUM---XExecution Pl ...

  2. C#中练级orcle数据查询

    直接贴代码哈哈哈, public DataTable getInfo(int flag) { OracleConnection conn = null; DataSet ds = new DataSe ...

  3. linux crontab创建计划任务

    1.编辑计划任务 编辑crontab文件 crontab -e 2.查看计划任务日志 查看crontab日志 tail -100f /var/log/cron 3.创建计划任务格式 (1)基本格式 : ...

  4. bat copy

    @echo off regedit /s %~dp0regedit.reg                                          //注册注册表xcopy "D: ...

  5. 扩展IHttpHandler

    前面提到当请求进入到IIS,IIS转发给ISAPI后会根据文件的后缀名来决定将请求转发给对应的处理程序进行处理,当然一大部分的处理都是交给了AspNet_ISAPI 了.但是,AspNet_ISAPI ...

  6. rsync_ssh

    rsync -av -e "ssh" /data/wwwroot/a1 node2:/data/wwwroot/

  7. php基础查找算法

    1.顺序查找 function line_search($array,$tar) { if(!is_array($array) || count($array) < 1) return fals ...

  8. 216种Web安全颜色

    216种Web安全颜色 全部 JavaScript HTML5 jQuery CSS EXT Ajax Web综合 界面设计 DWR   锁定老帖子 主题:216种Web安全颜色 精华帖 (0) :: ...

  9. li标签和checkbox绑定

    参考原文:https://www.cnblogs.com/youxin/p/3885496.html 我们经常需要li或span包含一个checkbox,不管点击checkbox或li都会触发相应的事 ...

  10. js正则表达式,只允许输入纯数字或‘/’

    //输入框,限数字和/----需要多个数量询价,请以/分隔 function onlyonce(obj) {//先把非数字的都替换掉,除了数字和.obj.value = obj.value.repla ...