[刷题]Binary Tree Maximum Path Sum II
2016-03-08 10:02
381 查看
[LintCode]Binary Tree Maximum Path Sum II
/** * 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 */ public int maxPathSum2(TreeNode root) { // 2016-03-08 // node value may <= 0 and the path contain at least one node if (root == null) { return 0; } int tempRst = sumHelper(root); if (tempRst == 0) { return root.val; } else { return tempRst; } } private int sumHelper(TreeNode root) { if (root == null) { return 0; } int leftSum = root.val + maxPathSum2(root.left); int rightSum = root.val + maxPathSum2(root.right); int bigger = Math.max(leftSum, rightSum); return Math.max(bigger, 0); } }
相关文章推荐
- 算法练习-excel地址转换
- runOnUiThread更新主线程
- Ubuntu gearman 搭建
- 【SPL标准库专题(1)】 SPL简介
- 安装sql2008的时候选择的是Windows身份验证,现在我想用sql server身份验证,我应该到哪儿设置呢
- iOS开发企业版ipa分发
- xib的简单使用
- LEETCODE 58
- 【汇总一】职称英语词汇
- TokuDB 文件目录
- 【囧】记人生的处女面。。。
- 分析ab在使用过程中的坑
- android graphic(11)—底层初始化displays
- (28)odoo中css可用颜色对照表
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)
- 矩阵的运算 --- 倍增法(UVA11149 - Power of Matrix)
- C++_const详解
- js获取当前日期时间及其它日期操作汇总
- Deep Residual Learning for Image Recognition 笔记
- 【bzoj1711】[Usaco2007 Open]Dingin吃饭 最大流