Problem Binary Tree Maximum Path Sum
2014-06-29 14:56
309 查看
Problem Description:
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
Solution:
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.
Solution:
public class Solution { public int max; public int maxPathSum(TreeNode root) { if (root == null) return 0; max = root.val; findMax(root); return max; } public int findMax(TreeNode node) { if (node == null) return 0; int left = Math.max(findMax(node.left), 0); int right = Math.max(findMax(node.right), 0); max = Math.max(node.val + left + right, max); return node.val + Math.max(left, right); } }
相关文章推荐
- 5.4.5 Binary Tree Maximum Path Sum
- leetcode_question_124 Binary Tree Maximum Path Sum
- Lintcode94 Binary Tree Maximum Path Sum solution 题解
- LeetCode: Binary Tree Maximum Path Sum
- leetcode Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 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
- Binary Tree Maximum Path Sum
- LeetCode(Nov 8 '12):Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum
- [Leetcode] Binary tree maximum path sum求二叉树最大路径和
- [leetcode]Binary Tree Maximum Path Sum @ Python
- Binary Tree Maximum Path Sum