LeetCode 124: Binary Tree Maximum Path Sum

/**
 * 124. Binary Tree Maximum Path Sum
 * 1. Time:O(n)  Space:O(logn)
 */

// 1. Time:O(n)  Space:O(logn)
class Solution {
    
    private int maxSum = Integer.MIN_VALUE;
    
    public int maxPathSum(TreeNode root) {
        helper(root);
        return maxSum;
    }
    
    private int helper(TreeNode root){
        if(root == null) return 0;
        int left = Math.max(helper(root.left),0);
        int right = Math.max(helper(root.right),0);
        maxSum = Math.max(maxSum,root.val+left+right);
        return root.val+Math.max(left,right);
    }
}
原文地址:https://www.cnblogs.com/AAAmsl/p/12855307.html