Binary Tree Maximum Path Sum
来源:互联网 发布:闪电抢购软件下载 编辑:程序博客网 时间:2024/04/28 15:45
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
第91个case过不了:
90 / 92 test cases passed.
Status: Wrong Answer
Submitted: 0 minutes ago
Input:{9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}Output:15Expected:16
但我把那个test case本地构建出来,返回的结果是正确的啊,我的程序的确返回了16。不知道为什么。
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private int res;public int maxPathSum(TreeNode root) {if (root == null) {return 0;} res = Integer.MIN_VALUE; longestPath(root); return res; }private int longestPath(TreeNode node){if (node == null) {return 0;}int left = longestPath(node.left);int right = longestPath(node.right);if (left < 0 && right < 0) {res = Math.max(res, node.val);}res = Math.max(left + right + node.val, res);if (left > right) {res = Math.max(left + node.val, res);return left + node.val;}else{res = Math.max(right + node.val, res);return right + node.val;}}}
0 0
- 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
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Linkedin - Returning Chinese Jobs Network 海归求职群
- 响应式布局的一些概念的整理
- 并查集(union-find)模板
- maysql有关取整及修改小数位值的问题
- 触发器的简单应用
- Binary Tree Maximum Path Sum
- 新年计划
- 如何彻底关闭Win8应用
- 失败者找借口,成功者找方法
- CRM上线之路 走上了CRM实施顾问-第112天上班 -第24周
- 恢复受损的Windows系统
- 用内置工具Word列算式
- 不要随意重装Windows操作系统
- 电脑突发故障应急处理方法