Binary Tree Maximum Path Sum [leetcode] dp
来源:互联网 发布:解密搜索器源码 编辑:程序博客网 时间:2024/06/09 19:36
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; }
0 0
- Binary Tree Maximum Path Sum [leetcode] dp
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- 【树形dp】Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- leetcode Binary Tree Maximum Path Sum
- 【leetcode】Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum
- [Leetcode]Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- LeetCode-Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- LeetCode 124: Binary Tree Maximum Path Sum
- how to learn
- UVA 10003 Cutting Sticks
- gcc 命令行写法
- FastReport经验
- 第七周项目3-求出两个整数的正差值
- Binary Tree Maximum Path Sum [leetcode] dp
- 主站sinox.org阻塞太厉害,大家用sinox.3322.org访问
- Linux系统下如何配置SSH?如何开启SSH?
- AdaBoost算法系列1
- OC-AutoLayout system UITableViewCell
- MFC对话框,接受键盘输入,并显示输入文本
- uva 10534 Wavio Sequence 高效lis onlgn
- HDU - 4800(dp递归边界仍存疑)
- 素数距离问题(南阳24)