a(i):在节点i由于单边路径的最大结束

b(i):在节点i路径和

a(i) = max{ i->val,
i->val + max{a(i->left), a(i->right) }};

b(i) = max{ i->val, i->val + max{a(i->left), a(i->right) } ,
i->val + a(i->left) + a(i->right)};

因为a(i), b(i)只和a(i->left)和a(i->right) 有关。因此能够将空间压缩为O(1)

代码例如以下:

    int maxPathSum(TreeNode *root) {
int res = INT_MIN;
getSum(root, res);
return res;
} int getSum(TreeNode * root, int & res)
{
if (root == NULL) return 0;
int l = getSum(root->left, res);
int r = getSum(root->right, res);
int a, b;
a = max(root->val, root->val + max(l, r));//one side
b = max(a, root->val + l + r); //both side
res = max(res, max(a, b));
return a;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Binary Tree Maximum Path Sum [leetcode] dp的更多相关文章

  1. Binary Tree Maximum Path Sum leetcode java

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

  2. Binary Tree Maximum Path Sum - LeetCode

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

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

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

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

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

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

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

随机推荐

  1. AutoFac使用方法总结:Part III

    生命周期 AutoFac中的生命周期概念非常重要,AutoFac也提供了强大的生命周期管理的能力. AutoFac定义了三种生命周期: Per Dependency Single Instance P ...

  2. Java RMI(远程方法调用) 实例与分析

    目的: 通过本文,可以加深对Java RMI的理解,知道它的工作原理,怎么使用等. 也为了加深我自己的理解,故整理成文.不足之处,还望指出. 概念解释: RMI(RemoteMethodInvocat ...

  3. oj 小黑熊偷玉米

    Description 小黑熊的邻居bob 家里种很多玉米,玉米被布置在一条线上 .小黑熊贪心要偷玉米.但bob家是太多了玉米,所以小黑熊决定选择时间间隔[l,r]偷.因为小黑熊的幸运号码是k,的区间 ...

  4. git 常用命令及问题解决(转)

    git init 产生的目录解释error: src refspec master does not match any.引起该错误的原因是,目录中没有文件,空目录是不能提交上去的error: ins ...

  5. JMS and ActiveMQ first lesson(转)

    JMS and ActiveMQ first lesson -- jms基础概念和应用场景 2011-6-18 PM 9:30 主讲:kimmking <kimmking@163.com> ...

  6. 解决新版Emacs的警告:Warning (initialization): Your load-path...

    升级到新版Emacs后出现警告 作为做好用的代码编辑器之一,Emacs绝对在极客世界实用率很高.当然VIM也有很多支持者.但小编是从VIM转到Emacs的,个人觉得Emacs更好用. 小编最近升级了F ...

  7. 笔试题&amp;面试题:输入一个维度,逆时针打印出一个指定矩阵

    称号:考虑到用户层面.打印出指定的矩阵,例如,一个给定的用户10,例如,下面的输出应被视为在图: 程序如下所示: #include <stdio.h> #include <mallo ...

  8. 从xcode 6 上传 App Store

    2014苹果结束了大会,ios8公布.可怜的苹果开发人员又要開始伤脑筋了. 比方提交新产品的那个iTunes connect体验就做得极烂.并且这还是本菜鸟的第一次上线提交.折寿啊 一.制作证书.ap ...

  9. ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

    原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性 深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开 ...

  10. Bulk Insert具体订单

    Bulk Insert具体订单 BULK INSERT与用户指定的格式的数据文件复制到数据库表或视图. 语法: BULK INSERT [ [ 'database_name'.][ 'owner' ] ...