[刷题]Binary Tree Maximum Path Sum
来源:互联网 发布:linux 数值运算 编辑:程序博客网 时间:2024/06/07 03:44
[LintCode]Binary Tree Maximum Path Sum
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root: The root of binary tree. * @return: An integer. */ private class RstType { int maxPath; int singlePath; public RstType(int s, int m) { singlePath = s; maxPath = m; } } private RstType helper(TreeNode root) { if (root == null) { return new RstType(0, Integer.MIN_VALUE); } // divide RstType left = helper(root.left); RstType right = helper(root.right); // conquer int singlePath = Math.max(0, root.val + Math.max(left.singlePath, right.singlePath)); int maxPath = Math.max(Math.max(left.maxPath, right.maxPath), root.val + left.singlePath + right.singlePath); return new RstType(singlePath, maxPath); } public int maxPathSum(TreeNode root) { // 2015-3-23 RstType rst = helper(root); return rst.maxPath; }}
0 0
- LeetCode刷题 | Binary Tree Maximum Path Sum
- [刷题]Binary Tree Maximum Path Sum
- [刷题]Binary Tree Maximum Path Sum II
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 泛函编程(14)-try to map them all
- Eclipse配置PyDev插件
- 9.3常用反义词组汇总表
- CentOS 6.4下部署oralce11g
- Linux限制用户行为
- [刷题]Binary Tree Maximum Path Sum
- [经典面试题]包含T全部元素的最小子窗口
- 【数位DP】 HDU 3709 Balanced Number
- jps命令使用
- 微信公众号接口添加菜单时错误(errcode":40017 invalid button type)
- 计蒜客 第21题:最大子阵列
- static 和 const
- 工作中jsonp 与ajax的封装
- Android自动登录 记住用户名密码实现 SharedPreferences