[Leetcode] 124. Binary Tree Maximum Path Sum

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.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxPathSum(TreeNode root) {
        if(root == null) return 0;
        ArrayList<Integer> result = new ArrayList<Integer>();
        result.add(Integer.MIN_VALUE);
        maxPath(root, result);
        return result.get(0);
    }
    private int maxPath(TreeNode root, ArrayList<Integer> result){
        if(root == null) return 0;
        int left = maxPath(root.left, result);
        int right = maxPath(root.right, result);
        int current = root.val + (left <= 0? 0: left) + (right <= 0? 0: right);
        if(current > result.get(0)) result.set(0, current);
        return root.val + Math.max(left, Math.max(0, right));
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章