Binary Tree Maximum Path Sum [leetcode] dp
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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- 【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 ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
随机推荐
- centos在设置时区
[root@localhost ~]# date -R // 查看时区 Mon, 19 May 2014 10:18:46 +0000 [root@localhost ~]# tzselect ...
- json2.js参考
json2.js使用參考 json2.js提供了json的序列化和反序列化方法,能够将一个json对象转换成json字符串,也能够将一个json字符串转换成一个json对象. <html> ...
- iSwifting如何发送照片社区
登录iSwifting社区 1,首先点击"帖子": 2,点击"照片": 3.点击"选择文件上传" 4,上传后的照片: 5,点击上传的照片: ...
- MVC快速分页
.NET手记-ASP.NET MVC快速分页的实现 对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实 ...
- Blend4精选案例图解教程(三):一键拖拽
原文:Blend4精选案例图解教程(三):一键拖拽 拖拽效果,常规实现方法是定义MoveLeftDwon.MoveLeftUp.MouseMove事件,在Blend的世界里,实现对象的拖拽,可以不写一 ...
- CentOS 安装memcached
一,安装libevent 安装位置 /usr/lib #tar -zxvf libevent-2.0.21-stable.tar.gz # cdlibevent-2.0.21 # ./configur ...
- CentOS7 已安装telnet维修
今天搞下 Centos 7 以下升级 openssl 和 openssh ,顺便说下安装 telnet # 设备 telnet 避 ssh 无法登录 yum -y install xinetd tel ...
- 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...
- 解决PhpCms V9后台无法上传图片
PHPCMS V9 在近期一次更新的版本号(9.4.2)中因为代码推断失误.导致PHPCMS在后台更新文章无法上传图片而导致的bug.在PHPCMS论坛中找到了暂时解决方式,希望PHPCMS官方能尽快 ...
- V微软S2015下载:开展Win10/Linux/iOS多平台软件
微软VS2015下载:开展Win10/Linux/iOS多平台软件 资源:IT之家作者:子非 责任编辑:子非 11月13日消息,微软刚刚宣布了 Visual Studio 2015 ...