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. jrtplib使用注意事项

    一.说明 RTP 现在的问题是要解决的流媒体的实时传输的问题的最佳方法.和JRTPLIB 是一个用C++语言实现的RTP库.包含UDP通讯.刚使用JRTPLIB,对JRTPLIB的理解还不够深,当做使 ...

  2. 双向链表实现简单的list

    双向链表结构: 定义一个如下结构体 struct Node { Object data; Node *next; Node *prev; }; 下面为list的具体实现: #include <i ...

  3. c++ primer 函数传值1

    不看c++ primer  永远不知道自己基础有多差 函数的參数传值一般有两种方式:值传递,引用传递. 值传递有以下两种形式: void func( int a ) { // } void func1 ...

  4. AsyncActivity异步加载网页

    import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; imp ...

  5. 用DOS命令获取文件列表

    其实就是两个命令:dir 跟 tree 在C:盘根目录下生成了一个名为“filelist.txt”的文本文件,该文件中即包含D:盘的文件夹列表. dir d:\ >c:\filelist.txt ...

  6. HDU 1864最大报销额(一维背包)

    题目地址:HDU 1864 刚上来看着挺麻烦的..细致看了看原来好简单好简单...仅仅要去掉一些不符合要求的发票,剩下的就是最简单的背包问题了..对于小数问题,仅仅要*100就变成整数了. 代码例如以 ...

  7. 初学者cocos2dx 写2048 为了和大家一起分享

    第一个是在头文件 部分的代码是学习不变  大多数写自己. class HelloWorld : public cocos2d::CCLayer { public: virtual bool init( ...

  8. [three.js] 地图不能解决重复的问题 Solving with Texture RepeatWrapping Fail Issue

    有些事情,如果你正在寻找侯,怎么也找不到. 有的东西,不经意间,到处: 我认为这是生活中常有的事. 然而,在互联网的浩瀚大海,这同样适用. 正常的一小会儿的积累, 投入少, 积累, 洋大海, 载起一帆 ...

  9. openstack临时存储后端

    声明: 本博客欢迎转发.但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 眼下openstack提供了 ...

  10. android大概是通过logcat拦截Log

    我们必须在系统的环境变量先增加adb 路径: 在原有环境的后面增加;E:\Android\android-sdk-r16\platform-tools(;是不能缺少的) 然后我们在cmd中输入adb, ...