Leetcode: Binary Tree Maximum Path Sum
2013-07-16 05:32
417 查看
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,
Return
Pay attention to this one.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / \ 2 3
Return
6.
Pay attention to this one.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int max; public int maxPathSum(TreeNode root) { // Start typing your Java solution below // DO NOT write main() function if(root == null) return 0; max = Integer.MIN_VALUE; get(root); return max; } public int get(TreeNode root){ if(root == null) return 0; int left = get(root.left); int right = get(root.right); if(left + right + root.val > max) max = left + right + root.val; int temp = Math.max(left, right); return temp + root.val > 0 ? temp + root.val : 0; } }
相关文章推荐
- LeetCode Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- [LeetCode][Java] Binary Tree Maximum Path Sum
- 【Leetcode】Binary Tree Maximum Path Sum
- leetcode_c++:树:Binary Tree Maximum Path Sum(124)
- Binary Tree Maximum Path Sum -- leetcode
- [LeetCode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum(最大路径和)
- [LeetCode] Binary Tree Maximum Path Sum
- Java for LeetCode 124 Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [leetcode]Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum --- LeetCode
- LeetCode之Binary Tree Maximum Path Sum
- leetcode第一刷_Binary Tree Maximum Path Sum
- leetcode -day9 Candy & Gas Station & Binary Tree Maximum Path Sum
- 【leetcode】binary-tree-maximum-path-sum
- LeetCode之Binary Tree Maximum Path Sum
- 【LeetCode】Binary Tree Maximum Path Sum